Reputation: 91
I am attempting to search a specific part of a text file and save the information in between two key words. What does the syntax look like to allow me to save only the information in the middle? Currently my code is:
$awk -f strip.awk <in>out
{
Strip.awk
{
if ($0 == "<!-- start of lyrics -->")
lyr = 1
if ($0=="<!-- end of lyrics -->")
lyr = 0
if(lyr==1)
Lyrics = lyr $0
}
printf("%s/n", Lyrics)
>>Lyric.csv
}
Upvotes: 1
Views: 139
Reputation: 3756
awk '/<!-- start of lyrics -->/,/<!-- end of lyrics -->/' input.txt >> Lyric.csv
This is from my copy of "The AWK Programming Language" (1984, p23)
:
- pattern 1 , pattern 2 { statements }
A range pattern matches each input line from a line matched by pattern 1 to the next line matched by pattern 2, inclusive; the statements are executed at each matching line.
A range pattern cannot be part of any other pattern.
To exclude pattern 1
& pattern 2
from the output range:
awk '/pattern 1/,/pattern 2/ {if ($0 !~ /pattern 1|pattern 2/) print}' input.txt
Upvotes: 4
Reputation: 77105
Both sed
and awk
supports regex ranges
.
$ cat ff
1
2
3
4
START
4
5
3
6
7
END
14
5
8
$ awk '/START/,/END/' ff
START
4
5
3
6
7
END
$ sed -n '/START/,/END/p' ff
START
4
5
3
6
7
END
Upvotes: 0
Reputation: 62389
It's not entirely clear what you mean by "save only the information in the middle", but assuming you mean you just want to print what's between the two delimiters:
awk '/<!-- start of lyrics -->/{p=1}/<!-- end of lyrics -->{p=0}p{print}0' input.txt
ought to work.
It basically sets and resets a flag depending on whether the start/end tags have been seen, and only prints lines when the flag is non-zero.
Upvotes: 0
Reputation: 124656
This script should do it:
#!/bin/sh
awk '
/<!-- start of lyrics -->/ { lyrics = 1; next }
/<!-- end of lyrics -->/ { exit }
lyrics { print }
'
If you call it script.sh
then you can use it like this:
./script.sh < input.txt > lyrics.txt
This is how it works:
/<!-- start of lyrics -->/ { lyrics = 1; next }
: If the line matches the start "pattern", then set the lyrics
variable and jump to the next line/<!-- end of lyrics -->/ { exit }
: If the line matches the end "pattern", then exitlyrics { print }
: If the lyrics
variable is set, print the lineUpvotes: 1