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:
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:
movie names:
Gravity
Faster
Turbo
^^^^^^^^^^^^^^^ regex should end here (blank line)
song titles:
Any ideas guys?
not sure what you want for the output, but here's the start:
awk -v RS='' '{print "record-> [" $0 "]\n"}' myFile
Thanks for the respone. The output would be like this
example A
ted
TMNT
Jinxed
example B
Gravity
Faster
Turbo
what OS are you on?
What version of awk are you using? gawk by any chance?
Im using open suse, awk version 4.1.0
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):
gawk '/^movie name/{n=split($0,a,ORS); if ($2=="names:") {for(i=2;i<=n;i++) print a} else {for(i=1;i<=n;i++) print substr(a,index(a,": ")+2)}}' RS='' myFile
1 Like
Her is another awk approach:
awk '/^movie name/ {
gsub("[^\n]+: *",x)
l=split($0,a,ORS)
for(i=1;i<=l;i++) if(a) print a
}' RS='' infile
2 Likes
Yet another one:
awk '/movie names/{$1=x; sub(FS,x); print}' FS='\n' OFS='\n' RS= file
or
awk '/movie names/{print substr($0,index($0,FS)+1)}' FS='\n' RS= file
or
awk '/movie names/{sub(/[^\n]*\n/,x); print}' FS='\n' RS= file
1 Like
With sed :
sed -n '/movie names:/,/^$/{//!p;}' file