Issue
I have a file which I'd like to process with bash. Can be with awk, sed or grep or similar. The file has multiple occurrences on a single line. I would like to extract everything between these two occurrences and print the output each on a separate line.
I have already tried using this:
cat file.txt | grep -o 'pattern1.*pattern2'
But this will print everything matching from pattern1 to the very last matching pattern2.
$ cat file.txt
pattern1 this is the first content pattern2 this is some other stuff pattern1 this is the second content pattern2 this is the end of the file.
I'd like to get:
pattern1 this is the first content pattern2
pattern1 this is the second content pattern2
Solution
This might work for you (GNU sed):
sed -n '/pattern1.*pattern2/{s/pattern1/\n&/;s/.*\n//;s/pattern2/&\n/;P;D}' file
Set the option -n
to print explicitly.
Only process lines that contain pattern1
followed by pattern2
.
Prepend a newline to pattern1
.
Remove upto and including the introduced newline.
Append a newline following pattern2
.
Print the first line in the pattern space, delete it and repeat.
Answered By - potong