आप किसी फ़ाइल को कैसे प्राप्त करते हैं और अगली 5 पंक्तियाँ प्राप्त करते हैं


117

मैं कैसे grepएक फ़ाइल के लिए 19:55लाइन 1,2,3,4,5 प्राप्त कर सकता हूं ?

2013/10/08 19:55:27.471
Line 1
Line 2
Line 3
Line 4
Line 5

2013/10/08 19:55:29.566
Line 1
Line 2
Line 3
Line 4
Line 5

2
आपका शीर्षक शानदार लग रहा है। तुम्हें पता है - नहीं, मैं एक फ़ाइल नहीं पकड़ता और इसे 5 पंक्तियाँ प्राप्त करता हूँ: p
अल्मा दो

जवाबों:


216

तुम्हें चाहिए:

grep -A 5 '19:55' file

से man grep:

Context Line Control

-A NUM, --after-context=NUM

Print NUM lines of trailing context after matching lines.  
Places a line containing a gup separator (described under --group-separator) 
between contiguous groups of matches.  With the -o or --only-matching
option, this has no effect and a warning is given.

-B NUM, --before-context=NUM

Print NUM lines of leading context before matching lines.  
Places a line containing a group separator (described under --group-separator) 
between contiguous groups of matches.  With the -o or --only-matching
option, this has no effect and a warning is given.

-C NUM, -NUM, --context=NUM

Print NUM lines of output context.  Places a line containing a group separator
(described under --group-separator) between contiguous groups of matches.  
With the -o or --only-matching option,  this  has  no effect and a warning
is given.

--group-separator=SEP

Use SEP as a group separator. By default SEP is double hyphen (--).

--no-group-separator

Use empty string as a group separator.

2
वाह वाह वाह। बहुत - बहुत धन्यवाद।
फिलिपमविकिकी

यह बहुत अच्छा होगा अगर आउटपुट को एक निश्चित मात्रा में लाइनों तक सीमित न करने का तरीका हो लेकिन मिलान के बाद सभी लाइनों को प्रिंट करें ।
मथायस ब्रौन

4

कुछ awkसंस्करण।

awk '/19:55/{c=5} c-->0'
awk '/19:55/{c=5} c && c--'

जब पैटर्न मिला, तो सेट करें कि c=5
क्या cसच है, प्रिंट करें और संख्या में कमी करेंc


हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.