|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am looking to create a script where I can do a search on a keyword and not only get the lines it shows up on but 3 lines above and below its. I am using HP-Unix. Grep on my machine does not have -A or -B. It may have sever occurance of the same search. I also plan to it to a temp file that I can read out of.
Thanks, ![]() |
|
#2
|
|||
|
|||
|
I have had to do something similar - what I did was to use the -n option (I think it was) of grep to return the line number, then use that (suitably added/subtracted from) with sed. I only had a small file so that was ok for me. If you have a big file you may wish to use another method.
awk may be of use - keep a rolling track of current line - 3, -2, and -1 and when you find your keyword print them, and following 3 lines out. |
|
#3
|
|||
|
|||
|
A barbarian way of doing this could be to write each line of your input into a work file, and grep -q on each one read this way. If the grep is successful, you can print the tail -3 of the workfile.
Not as quick as an awk call I´m afraid. Well, I said it was barbarian. ![]() |
|
#4
|
|||
|
|||
|
Not sure I understood what you wanted but
Code:
sed -n -e '/Bad/{=;1!p;g;$!N;p;D;}' -e h filename
on this data: Code:
hello hi *User Bad *Morning Good Bye Code:
4 Bad *User *Morning |
|
#5
|
|||
|
|||
|
Thinking about, changed my mind. Easy to change small things
![]() Using the example file above: Code:
grep -A1 -B1 'Bad' filename seems to be what you're asking for... |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Getting line surrounding your grep keyword |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|