Grep for the string and then print the next 4 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for the string and then print the next 4 lines
# 1  
Old 05-16-2013
Grep for the string and then print the next 4 lines

RHEL 5.8

I have a text file like below. I want to grep for a string and then print the next 4 lines including the line with the string I grepped for

For eg:

I want grep for the string HANS and then print the next 4 lines including HANS

Code:
$ cat someText.txt
JOHN
        NATIONALITY: KENYAN
        OCCUPATION : ACCOUNTANT
        AGE                : 35
MARTHA
        NATIONALITY: BRAZILIAN
        OCCUPATION : LAWYER
        AGE                : 25
HANS
        NATIONALITY: GERMAN
        OCCUPATION : ENGINEER
        AGE                : 29
ALEX
        NATIONALITY: CANADIAN
        OCCUPATION : CHARTERED ACCOUNTANT
        AGE                : 45
KRISHNA
        NATIONALITY: INDIAN
        OCCUPATION : RESEARCHER
        AGE                : 35
$
$
$ grep -n HANS someText.txt   ----------> String HANS is in the 9th line
9:HANS

I want the output to be
Code:
HANS
        NATIONALITY: GERMAN
        OCCUPATION : ENGINEER
        AGE                : 29

This User Gave Thanks to omega3 For This Post:
# 2  
Old 05-16-2013
Code:
grep -A3 HANS someText.txt
HANS
        NATIONALITY: GERMAN
        OCCUPATION : ENGINEER
        AGE                : 29

Code:
awk '/HANS/ {print;getline;print;getline;print;getline;print}' someText.txt

These 2 Users Gave Thanks to Jotne For This Post:
# 3  
Old 05-16-2013
sophisticated command would be Smilie

Code:
 
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=3 s="HANS" filename

here b means number of lines above the pattern.. a means below and s for pattern
This User Gave Thanks to vidyadhar85 For This Post:
# 4  
Old 05-16-2013
More simple awk
Code:
awk '/HANS/ {f=4} f>0 {print;--f}' someText.txt

This User Gave Thanks to Jotne For This Post:
# 5  
Old 05-16-2013
$ grep -A3 Akhil sample.txt
Akhil
Good
better
best
This User Gave Thanks to Gangi Reddy For This Post:
# 6  
Old 05-16-2013
@Gangi
Same as I have written in post #2


@vidyadhar85
Quote:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=3 s="HANS" filename
=
Code:
grep -A3 -B0 "HANS" filename

PS your line is nearly impossible to understand. Smilie
So I made a version I understand...
Code:
awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-B;i<=f+A;i++) print a[i]}' B=2 A=3 s="HANS"


Last edited by Jotne; 05-16-2013 at 02:33 PM..
This User Gave Thanks to Jotne For This Post:
# 7  
Old 05-16-2013
The following identifies the record end by the next matching ^[A-Z]
Code:
awk '/^[A-Z]/ {pr=0} $0~"^"key" *$" {pr=1} pr' key=KRISHNA someText.txt

Further it matches the key at the beginning, so does not misbehave with key=NATION
Probably easier, an exact string match instead of an ERE match:
Code:
awk '/^[A-Z]/ {pr=0} $0==key {pr=1} pr' key=KRISHNA someText.txt


Last edited by MadeInGermany; 05-16-2013 at 02:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

4. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

6. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

7. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question