Computing.Net > Forums > Unix > Grep Command Help

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

Reply to Message Icon

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


Could any one help me getting this result

Thanks and Regards
Chinmaya



Sponsored Link
Ads by Google

Response Number 1
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

Related Posts

See More



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.

Thanks and Regards
Chinmaya


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Grep Command Help

Help on grep command www.computing.net/answers/unix/help-on-grep-command/7376.html

grep command www.computing.net/answers/unix/grep-command/6034.html

Using the grep command www.computing.net/answers/unix/using-the-grep-command/4811.html