Extract paragraphs under conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract paragraphs under conditions
# 1  
Old 05-07-2012
Data Extract paragraphs under conditions

Hi all,
I want to extract some paragraphs out of a file under certain conditions.
- The paragraph must start with 'fmri'
- The paragraph must contain the string 'restarter svc:/system/svc/restarter:default'

My input is like that :
Code:
fmri         svc:/system/vxpbx:default
state_time   Wed Mar 21 08:49:43 2012
logfile      /var/svc/log/system-vxpbx:default.log
restarter    svc:/system/svc/restarter:default
dependency   require_all/none svc:/system/filesystem/local (online)
 
fmri         svc:/application/management/common-agent-container-1:default
name         Cacao, a common Java container for JDMK/JMX based management solution
enabled      false
dependency   require_all/none svc:/milestone/multi-user (online)

And I should have this in output :
Code:
fmri         svc:/system/vxpbx:default
state_time   Wed Mar 21 08:49:43 2012
logfile      /var/svc/log/system-vxpbx:default.log
restarter    svc:/system/svc/restarter:default
dependency   require_all/none svc:/system/filesystem/local (online)

I came up to the following solution but the issue I have is that only the first paragraph is displayed. I guess it's due to the fact that the command does not loop and stops at the first occurence found :
Code:
awk '/fmri/{s=x}{s=s$0"\n"}/restarter/{p=1}/^$/ && p{print s;exit}' svcs-l_spsol03.out

Thanks for your help !
# 2  
Old 05-07-2012
Try:
Code:
awk -vRS= '/^fmri/&&/restarter/' file

# 3  
Old 05-07-2012
Didn't think about this vRS possibility, thanks a lot !
# 4  
Old 05-07-2012
the command's not vrs. the -v specifies variable, RS specifies record separator. When it's empty, awk splits on blank lines.
# 5  
Old 05-07-2012
Quote:
Originally Posted by Corona688
the command's not vrs. the -v specifies variable, RS specifies record separator. When it's empty, awk splits on blank lines.
Thanks for your comment
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract paragraphs and count them

Hi, I have a text with a number of paragraphs in them. My problem is I need to locate certain errors/warning and extract/count them. Problem is I do not know how many paras are there with that particular type of error/warning. I had thought that somehow if I could count the number of... (25 Replies)
Discussion started by: dsid
25 Replies

2. Shell Programming and Scripting

Extract duplicate rows with conditions

Gents Can you help please. Input file 5490921425 1 7 1310342 54909214251 5490921425 2 1 1 54909214252 5491120937 1 1 3 54911209371 5491120937 3 1 1 54911209373 5491320785 1 ... (4 Replies)
Discussion started by: jiam912
4 Replies

3. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

4. Shell Programming and Scripting

Extract lines between tags and conditions

Hi forum, I need help .. I need to extract from the log file logfile.log all the rows between the tags "<SOAP-ENV: Envelope" "</ SOAP-ENV: Envelope>" and that contain a certain <idPrescrizione> 0FXTN091GI </ idPrescrizione>. Within logfile.log there could be more 'occurrences of the same ... (3 Replies)
Discussion started by: mannone
3 Replies

5. Shell Programming and Scripting

Need help with sorting in paragraphs

I am very new to shell scripting, current try to do a sorting of a text file in paragraphs with ksh script. example: File content: A1100001 line 1 = "testing" line 2 = something, line 3 = 100 D1200003 line 1 = "testing" line 2 = something, line 3 = 100 B1200003 line 1 =... (3 Replies)
Discussion started by: gavin_L
3 Replies

6. Shell Programming and Scripting

Split text into paragraphs

Hi all! I want to make a code to split sentences into paragraphs maybe 4-5 sentences into one <p>text</p> there are no new lines in the text string any ideas with AWK, SSH? Thank you! (5 Replies)
Discussion started by: sanantonio7777
5 Replies

7. Shell Programming and Scripting

Extract file records based on some field conditions

Hello Friends, I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited): ColA|ColB|ColC|ColD|ColE|ColF Now for this file, I have to get those records which fulfil the following condition: If "ColB" is NOT NULL and "ColD" has values one of the following... (9 Replies)
Discussion started by: mehimadri
9 Replies

8. Shell Programming and Scripting

extract lines based on few conditions

Hi, I need to extract lines based on some conditions as explained below: File format details: notes: 1. each set starts with AAA only 2. number of columns is fixed 3. number of rows per set may vary (as one set is upto DDD - 4 rows) Now, if any BBB's 5th column is blank then then... (4 Replies)
Discussion started by: prvnrk
4 Replies

9. Shell Programming and Scripting

how to filter out some paragraphs in a file

Hi, I am trying to filter out those paragraphs that contains 'CONNECT', 'alter system switch logfile'. That means say the input file is : ------------------------------------------------------- Wed Jun 7 00:32:31 2006 ACTION : 'CONNECT' CLIENT USER: prdadm CLIENT TERMINAL: Wed Jun 7... (7 Replies)
Discussion started by: cnlhap
7 Replies

10. Shell Programming and Scripting

how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!!

I]hi all i am in confusion since last 2 days :( i posted thraed yesterday and some friends did help but still i couldnt get solution to my problem let it be very clear i have a long log file of alkatel switch and i have to seperate the minor major and critical alarms shown by ! , !! and !!!... (6 Replies)
Discussion started by: nabmufti
6 Replies
Login or Register to Ask a Question