Regex to include up to blank line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex to include up to blank line.
# 1  
Old 10-08-2014
Regex to include up to blank line.

Hi guys I am trying to figure out how to match a pattern with a regex up to a full blank line. I will show you what I mean with this example:

Code:
example A
movie name: ted
movie name: TMNT
movie name: Jinxed

example B
movie names:
Gravity
Faster
Turbo

song titles:
dont
hello
problem

So above example A seems ok to do, just match "name:". But example B the names are on the next line so I was thinking if I was able to match "names{0,1}:" up to a full blank line so it would go up as far as this:
Code:
movie names:
Gravity
Faster
Turbo

^^^^^^^^^^^^^^^ regex should end here (blank line)
song titles:

Any ideas guys?
# 2  
Old 10-08-2014
not sure what you want for the output, but here's the start:
Code:
awk -v RS='' '{print "record-> [" $0 "]\n"}' myFile

# 3  
Old 10-08-2014
Thanks for the respone. The output would be like this
Code:
example A

ted
TMNT
Jinxed

example B

Gravity
Faster
Turbo

# 4  
Old 10-08-2014
what OS are you on?
What version of awk are you using? gawk by any chance?
# 5  
Old 10-08-2014
Im using open suse, awk version 4.1.0
# 6  
Old 10-08-2014
you have inconsistent format of the 'movie names' and "move name". Here's how to accommodate both. (Somehow gawk's RS='' doesn't work as described in 'man gawk' - makes the code a bit hairy):
Code:
 gawk '/^movie name/{n=split($0,a,ORS); if ($2=="names:") {for(i=2;i<=n;i++) print a[i]} else {for(i=1;i<=n;i++) print substr(a[i],index(a[i],": ")+2)}}' RS='' myFile

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 10-08-2014
Her is another awk approach:

Code:
awk '/^movie name/ {
   gsub("[^\n]+: *",x)
   l=split($0,a,ORS)
   for(i=1;i<=l;i++) if(a[i]) print a[i]
}' RS='' infile

These 2 Users Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

3. Shell Programming and Scripting

awk regex- include text

Hi I am trying to filter some data using awk. I have a statement- awk 'BEGIN { FS = "\n" ; RS = "" } { if ( $6 = "City: " ) { print "City: Unknown" } else { print $6 } }'` The $6 values are City: London City: Madrid City: City: Tokyo This expression seems to catch all the lines... (4 Replies)
Discussion started by: jamie_123
4 Replies

4. Shell Programming and Scripting

Need sed help: find regex and if the next next line is blank, delete both

I've got a report I need to make easier to read Using sh on HP-UX 11.12. In short, I want to search for a regular expression and when found, examine the next line to see if it's blank. If so, then delete both lines. If not blank, move on to the next regexp. Repeat. So far I've got: ... (7 Replies)
Discussion started by: Scottie1954
7 Replies

5. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

6. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

7. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

8. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

9. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question