Search string and print the above line and below lines?.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search string and print the above line and below lines?.
# 1  
Old 11-26-2012
Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches.



Input
------

TranTime 2012 10 12
The Record starts here
Accountnumber: 4632473431274
TxnCode 323
TranID 329473242834
ccsdkcnsdncskd wdededadlksadksadaskdsakd
dadscczxczccc
Balance 2000.00
TranTime 2012 10 13
The Record starts here
Accountnumber: 5435478454
TxnCode 312
TranID 5634875438
Balance 4000.00
csdhcsdjcsdcsdcnskjcjsdkjsdj
TranTime 2012 10 15
The Record starts here
Accountnumber: 31231231232
TxnCode 212
sdjhcsdjcbsd csdmcndscndskc
TranID 6546754654
Balance 1000.00



Here is what i have tried so far. but no luck.

awk '{
if ($0 ~ /The Record starts here/)
{
print a;
}
{
a = $0;
}
else
if ($0 ~ /TranID|Balance|Accountnumber/)
{
print;
}
else
{
print "else"
}
}'


Expected output.


TranTime 2012 10 12
The Record starts here
Accountnumber: 4632473431274
TranID 329473242834
Balance 2000.00
TranTime 2012 10 13
The Record starts here
Accountnumber: 5435478454
TranID 5634875438
Balance 4000.00
TranTime 2012 10 15
The Record starts here
Accountnumber: 31231231232
TranID 6546754654
Balance 1000.00


any help will be greatly appreciated.

Thanks.
# 2  
Old 11-26-2012
gnu grep (ggrep on solaris) provides what you need.

What OS are you on
Code:
uname -a

# 3  
Old 11-27-2012
AIX 6.1
# 4  
Old 11-27-2012
try:
Code:
awk '/^The Record/{$0=p$0}/^TranTime/{p=$0"\n"}/The Record/||/^Accountnumber/||/^TranID/||/^Balance/ {print}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 11-27-2012
Thank you for your time. i will check.
# 6  
Old 11-27-2012
cat input.file | awk '{ if ($0 ~ /Record|TranTime|TranID|Balance|Account/) print $0; }'
What about the command above?
# 7  
Old 11-27-2012
Command above can be written.
Code:
awk '$0 ~ /Record|TranTime|TranID|Balance|Account/' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

2. Shell Programming and Scripting

Search words in a line and print next 15 lines.

I have a text file ( basically a log file) and i have 2 words (alpha, beta), Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines. ... (4 Replies)
Discussion started by: kashif.live
4 Replies

3. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

4. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

5. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

6. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

7. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

Search a string and print the rest of line

Hi Guys, I need to search a string and print the rest of the lines... input: 8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n output: 00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n Actually i don even need the first "00".. any suggestions is appreciated..... (13 Replies)
Discussion started by: mac4rfree
13 Replies

9. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

10. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question