Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Grep Command Help
Name: Chinmaya Date: August 16, 2005 at 03:10:33 Pacific OS: HP-UX CPU/Ram: NIL
Comment:
Hi All,
In a file I want to grep a string. I want to get a result not only greping line but also the 4 lines below it.
Example:
File A contains following lines
ABC 123 11/07/05 MONDAY FROM LEICESTER UK XYZ 123 01/07/05 FRIDAY FROM BANGALORE INDIA ABC 222 16/08/05 TUESDAY FROM LONDON UK
After I grep string ‘ABC’ the result file should contain following lines
ABC 123 11/07/05 MONDAY FROM LEICESTER UK ABC 222 16/08/05 TUESDAY FROM LONDON UK
Name: Luke Chi Date: August 16, 2005 at 05:16:37 Pacific
Reply:
#!/usr/bin/sh
CT=0 cat 5 | while read LINE do if [ "$CT" -gt 0 ]; then echo $LINE CT=`expr $CT - 1` continue fi echo $LINE | grep $1 > /dev/null 2>&1 if [ $? -eq 0 ]; then echo $LINE CT=3 fi done
Luke Chi
0
Response Number 2
Name: Luke Chi Date: August 16, 2005 at 05:20:11 Pacific
Reply:
Forgot one thing:
You need to pass the search string, for example:
$ program.sh ABC
Luke Chi
0
Response Number 3
Name: Luke Chi Date: August 16, 2005 at 05:40:32 Pacific
Reply:
Forgot another thing:
cat 5 <===== 5 is your input filename
Luke Chi
0
Response Number 4
Name: aka Date: August 18, 2005 at 13:59:44 Pacific
Reply:
Do this
grep -A9 ABC (file)
Don´t freak out until you know the facts...Relax...!
0
Response Number 5
Name: aka Date: August 18, 2005 at 14:01:44 Pacific
Reply:
Adding
-A9 means 9 files AFTER ABC
Try, and let me know
Don´t freak out until you know the facts...Relax...!
0
Response Number 6
Name: Chinmaya Date: August 21, 2005 at 07:55:23 Pacific
Reply:
Thanks a lot Luke and aka for your scripts and commands. I could get my intended result by script.
aka, the command grep -A9 ABC (file) is not working.
Summary: Hi Suppose a file F1 conatins following entries ABCD0B ABCD0Bs ABCD0B.ksh I would like to grep the word that contains only '0B' as last 2 charcaters. When I tried grep command : grep '0B' F1 it resu...
Summary: I am only fairly new to unix, so i have a question about grep.... i use the grep command at work to search for particular records. i use the grep command as "grep <criteria> <filename>". i know...
Summary: Using the KORN SHELL ONLY, is there any way that I can grep for a string in column/field 1 only? For instance in a data file named inventory, i have: black goodyear tire black michelin tire film win...