Shell process lines of code

Hi,

I would like to see if it is possible to process the output of a grep in this way.

sample command: command | grep -B4 "search string found on line 5"


Sample input from stdout:
(Line 1) Random #
Line 2
Line 3
Line 4
Line 5

I've been reading man pages/searching/googling for a way to grab only line 1 from the output using awk, grep, sed or xargs. (or something else).

If possible, is there a way to grab line1 based on the number or if line 1 was text, then just grab line 1 off of the match?
 
Pseudo-code: command | grep -B4 "search string found on line 5" | head -1

The head -1 will only show the first line, see head(1). There's also a tail(1) command to get just the last line(s) but is more commonly used to 'tail' or 'follow' a file, i.e. tail -f /var/log/messages
 
Thanks SirDice,

that does work, how about if you get multiple results (for example searching through log files). the head -1 / n1 will only bring back the first match if there are multiple lines.. What if I wanted to bring back line one from each match.


What if the sample output is this:

Sample input from stdout:
match 1 - (Line 1) Random #
match 1 - Line 2
match 1 - Line 3
match 1 - Line 4
match 1 - Line 5

match 2 - (Line 1) Random #
match 2 - Line 2
match 2 - Line 3
match 2 - Line 4
match 2 - Line 5

match 3 - (Line 1) Random #
match 3 - Line 2
match 3 - Line 3
match 3 - Line 4
match 3 - Line 5
 
Repeat that procedure for each file, i.e:
Code:
cd /var/log

for file in *; do
   grep -B4 "search string found on line 5" ${file} | head -1
done
If there are files and directories mixed, you can use find(1):
find <path> -type f -exec grep -B4 "search string found on line 5" {} | head -1 \;
 
I've been reading man pages/searching/googling for a way to grab only line 1 from the output using awk, grep, sed or xargs. (or something else).


command | awk 'NR==1 { print }' # if Number of Row Record ==1 print it out (but it goes through any onther line as well, so it's not very efficient)
command | grep -m 1 '' # match anything and stop after first match (-m or --max-count)
command | sed 1q # print first line and quit

The sed solution is the fastest (and shortest). This function of sed is BTW the reason why there was only a tail-command, but no head for quite a while - why bother writing another tool if sed Nq does the job just as well? ;)
 
thanks everyone,

I'm trying to take output from something like
Code:
qmHandle -l | grep  -B4  <grep_search>
. However, when I use xargs it processes each line when I pass it to sed 1q and gets output such "no such file".. I was hoping to process each 5 line match output as a block...

back to the drawing board.
 
If I understand you correctly you can use a combination of paste(1) and cut(1):
Code:
qmHandle -l | grep -B4 <grep-search> | paste - - - - - - | cut -f 1
You need 6 -'s in order to merge the 5 output lines + the block separator line created by grep (--) into one single line. The original lines are separated by tabs which makes it easy for cut to get the first line.
 
Back
Top