search and retrieve previous line in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and retrieve previous line in file
# 1  
Old 02-23-2007
search and retrieve previous line in file

I've got a file with the following layout:

#STMP FSgroup
filename /filesysname1
filestatus 2

#STMP FSstatus
filename /filesysname1
ratio 30

#STMP FSgroup
filename /filesysname1
filestatus 2

#STMP FSstatus
filename /filesysname6
ratio 60

I'm trying to write a SHELL (ksh) script that'll search the file say e.g filename = /filesysname1 grouped under FSstatus and get everthing beneath FSstatus group.

Thus, I should be able search for anygroup, i.e. lines that start with an # and everything below it until the first blank line. I also need to know the group name, i.e. either FSstatus or FSgroup

Look really difficult for me, any help out there

tks
William Smilie
# 2  
Old 02-23-2007
Will this be ok?

Code:
var=filesysname1
awk -F" " 'BEGIN{group=""; x=0} { if( x == 1 ) { print } if( $0 ~ /^#/) { group = $0; next } if( $0 ~ /'$var'/ ) { x=1; print group; print $0} else { x = 0 } }' file

# 3  
Old 02-23-2007
Quote:
I'm trying to write a SHELL (ksh) script that'll search the file say e.g filename = /filesysname1 grouped under FSstatus and get everthing beneath FSstatus group.
If all the groups are separated by blank line you can use this
Code:
awk -v RS="" -v FS="\n" -v ORS="\n\n" ' $1 ~ "FSstatus" && $0 ~ "/filesysname1" ' file

Quote:
Thus, I should be able search for anygroup, i.e. lines that start with an # and everything below it until the first blank line. I also need to know the group name, i.e. either FSstatus or FSgroup
If all the groups are separated by blank line you can use this to display group with group name "FSstatus"
Code:
awk -v RS="" -v FS="\n" -v ORS="\n\n" ' $1 ~ "FSstatus" ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Retrieve logs for previous 4 hours

Hi, I am in the process of configuring a script, and i intend it to retrieve logs for previous four hours, and then scan for predefined errors. I am kind of stuck on the log retrieval part where the script will run early morning like 1 AM or 2 AM, the command as posted below will give me... (4 Replies)
Discussion started by: john_prince
4 Replies

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

6. Shell Programming and Scripting

solved -gawk, search for pattern - mark the previous line as a variable?

Im trying to parse ifconfig with awk and setup a bunch of variables in one shot. But Im having trouble figuring out how to work with data in previous lines. ifconfig output: eth0 Link encap:Ethernet HWaddr 00:50:DA:10:7F:1B inet addr:10.10.10.10 Bcast:10.10.10.127 ... (0 Replies)
Discussion started by: trey85stang
0 Replies

7. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

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

9. Shell Programming and Scripting

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. (6 Replies)
Discussion started by: kamranjalal
6 Replies

10. UNIX for Dummies Questions & Answers

How to retrieve a particular line from a file

Hi, I want to retrieve a particular line from a file and print its content to a file. Can you suggest how to do it in UNIX ?. Rgds vinayap (2 Replies)
Discussion started by: vinayap
2 Replies
Login or Register to Ask a Question