Printing paragraph between semicolons having XXXX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing paragraph between semicolons having XXXX
# 1  
Old 09-16-2014
Wrench Printing paragraph between semicolons having XXXX

Hello experts,

I have below file:
Code:
1
2
3
4
5
6
;
7
8
9
10
XXXX
1
;
2
3
4
5
6
;
7
8
XXXX
9
;
10
1
2
3
4
;
5
XXXX
6
7
;
8
9
10
1
2
3
4
5
;
YYYY
6
7
;
8
9
10

I would like to print the paragraph between semicolons having XXXX (not YYYY). The idea is to match a pattern XXXX and print its respective segment which is enclosed in semicolons. So the required output of the above file will be as follows:
Code:
;
7
8
9
10
XXXX
1
;
;
7
8
XXXX
9
;
;
5
XXXX
6
7
;

Anything in sed/awk/perl would be a great idea.

Thanks ,
Manu


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 09-16-2014 at 10:13 AM..
# 2  
Old 09-16-2014
Hello manuswami,

Kindly use the code tags while posting commands and codes. Followng may help you in same.

Code:
awk '($0 ~ /;/){set++;} set{v=v?v ORS $0:$0} (v ~ /XXXX/){value=1} {if(set>1 && value==1){print v;v="";value="";set=""}}'  filename

Output will be as follows.

Code:
;
7
8
9
10
XXXX
1
;
;
7
8
XXXX
9
;
;
5
XXXX
6
7
;


Thanks,
R. Singh
# 3  
Old 09-16-2014
Try

Code:
$ awk 'function dothis(){ if(v && s==2){ print p} if(s==2)s=v=p="" }{ dothis(); s += /;/ ; v += /XXXX/; s ? p = length(p) ? p ORS $0 : $0 : "" }END{dothis()}' file


Last edited by Akshay Hegde; 09-16-2014 at 10:39 AM..
# 4  
Old 09-16-2014
Try (untested)
Code:
awk '!/XXXX/ {$0=""}1 ' RS=";" ORS";" file

# 5  
Old 09-16-2014
Code:
awk 'NR>1{if(/XXXX/) print RS $0} END{print ";\n"}' RS=\; ORS= file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep file and print value only if the following line doesnt contain value xxxx

Hi All, I have some large files which I would like to obtain some data from, I want to grep (or alturnative method) out certain values hut only if the folowing line doesnt contain a certain value. I will explain better below showing an example print of the data. NE : V0001 NE : V0002... (6 Replies)
Discussion started by: mutley2202
6 Replies

2. Shell Programming and Scripting

Replace semicolons with tabulators, new lines are disappearing

Hi Gurus! Example file: 1;AAA;BBB 2;CCC;DDD We want to replace semicolons to tabulators. Like this: 1 AAA BBB 2 CCC DDD We have tried these codes. With PERL: #!/bin/bash for i in `find /folder1/ -name "*.CSV"` do bi="`basename $i awk -F"." {'print $1'}`" cat... (2 Replies)
Discussion started by: JanneN
2 Replies

3. UNIX for Dummies Questions & Answers

2 semicolons

Just wondering what 2 semicolons together after a command means (2 Replies)
Discussion started by: millsy5
2 Replies

4. Shell Programming and Scripting

Removal of last-semicolons in line with sed

Hello, I'm trying to remove an arbitrary number of semicolons at the end of each line in the input file. Input: 44;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;; 45;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;;;; ... (6 Replies)
Discussion started by: uioreanu
6 Replies

5. Shell Programming and Scripting

Searching for a pattern between 2 semicolons

I have logs which is having strings like below, errorMsg;; errorMsg;bad input; so I need to grep from logs file which searches for errorMsg followed by semicolon followed by any text and then ending again with semicolon. More importantly, I should not get errorMsg and having nothing... (4 Replies)
Discussion started by: gopikrish81
4 Replies

6. Shell Programming and Scripting

Adding semicolons

Lets say I wanted to add a ; before the last 6 characters of my variable how would I do this? (2 Replies)
Discussion started by: puttster
2 Replies

7. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

8. UNIX for Dummies Questions & Answers

Script or SED command for [[Xxxx Xxxx Xxxx]] to [[Xxxx xxx xx]]

Hi, To comply with a new naming convention on a mediawiki site we have to run a SED or other PERL command to change all instances of ] or ] or ] to ] Can someone please explain how to do this... It has to be done on a mysql dump, so if there is a way to do this in mysql even... (2 Replies)
Discussion started by: lawstudent
2 Replies

9. UNIX for Dummies Questions & Answers

Removing spaces between XML tags<XX XX> -> <XXXX>

hey guys, i have an XML like this: <documents> <document> <Object ID>100114699999</Object ID> <Object Create Date Time>2008-04-07T00:00:00</Object Create Date Time> </document> <documents> I need all my tags within the XML to not include any spaces. i.e. everything between <t a g> in... (8 Replies)
Discussion started by: sharoff
8 Replies

10. UNIX for Dummies Questions & Answers

rm: Unable to remove directory xxxx/xxxx: File exists

Hi Everyone, I am having problem to delete an "empty" folder ( messages attached ). It displays "total 12" when i typed "ls -lart" on the fnxroot44 folder, but i can't view any file. Is there any way to view those unseen files ? I don't know why option "a" is not working this time. Would... (1 Reply)
Discussion started by: deejay
1 Replies
Login or Register to Ask a Question