Search text from a file and print text and one previous line too


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search text from a file and print text and one previous line too
# 1  
Old 01-02-2009
Search text from a file and print text and one previous line too

Hi,

Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11

Thanks for your help.
# 2  
Old 01-02-2009
One way:

Code:
awk '/pattern/{print s "\n" $0;exit}{s=$0}' file

Regards
# 3  
Old 01-02-2009
I have no idea what HP-UX 11.11 is.. but normal *nix shell, you can do:

grep -A2 "sometext" somefile.ext

It would grab that text, and the two lines afterwards.. You can also use -B for before.
# 4  
Old 01-02-2009
Or:

Code:
grep -B1 pattern file

or:

Code:
sed -n -e '/pattern/{x;p;x;p}' -e h file

# 5  
Old 01-02-2009
Please "cfajohnson" What should I change in the following command to find text and its previous line.

I got command (text=start; awk '/'"$text"'/{n=2}n-->0' filename) for finding text and next line.


Thanks for your help.
# 6  
Old 01-05-2009
Hi,

Command awk '/pattern/{print s "\n" $0;exit}{s=$0}' file only returns the first occurance of the pattern, it is not showing the next occurance.

Please help me, thanks
# 7  
Old 01-06-2009
Remove the exit command:

Code:
awk '/pattern/{print s "\n" $0}{s=$0}' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

4. UNIX for Dummies Questions & Answers

how to print out consecutive two line from a text file

the text file like below's input: aaaaaaaaafdsfsda sdfsadfasdfasfds sdfadsf asdfadf asdfa adfsfsafas sdfsfafads asdfasdfsa I want to print out consecutive two line output: aaaaaaaaafdsfsda (1 Reply)
Discussion started by: vincent_W
1 Replies

5. UNIX for Dummies Questions & Answers

search all file for particular text and make changes to line 3

Hi All, I am sitting on HPUX. I want to change the exit into #exit, which appears into 3red line of code in shell scripting, wondering how shell script to be called up to perform action. I have following code in all files. Now, I need to find the text exit and replace into #exit. #!/sbin/sh... (10 Replies)
Discussion started by: alok.behria
10 Replies

6. 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

7. Shell Programming and Scripting

reverse search a text file from a specified line

Hello All, I have a following task that I need to accomplish through a script or program and I am looking for some help as I have exhausted my ideas. 1. given: a text file with thousands of lines 2. find: pattern A in file and get line number ( grep -n works) 3. find: the first occurence of... (14 Replies)
Discussion started by: PacificWonder
14 Replies

8. Shell Programming and Scripting

need to search text and output previous lines

I have a file (OMlog0) that is quite large, I need to find the line "Automatic Recharge Audit Process Finished" and output that line and the time stamp that occurs two lines previous. The line that I was using is "sed -n '/Automatic Recharge Audit Process Finished/,/No errors/p' /sn/log/OM* >... (8 Replies)
Discussion started by: grinds
8 Replies

9. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies

10. Shell Programming and Scripting

Search for text and print the next line

Hi, I want to write a small script to search for a text in file and when its found I want to print the next line. I try to write that script but I could not manage it, I just write the following script the find the exact line but I want the next line. $ sed -n -e '/Form not/p' test.txt... (2 Replies)
Discussion started by: alijassim
2 Replies
Login or Register to Ask a Question