0

I have a m3u file as below. I want to delete all strings except the pattern and next first line. or save the pattern and the next first line as another file.

Patterns group-title="TR ULUSAL" , group-title="TR HABER" , group-title="TR BELGESEL" Raw data:

#EXTINF:-1 tvg-id="" tvg-name="TRT HD 1080" tvg-logo="http://url/tr/trt.png" group-title="TR ULUSAL",TRT HD 1080
http://xxx.xx:xx/xx/xx/123

#EXTINF:-1 tvg-id="" tvg-name="THRILLER 16 Blocks" tvg-logo="http://url/Blocks.jpg" group-title="DEUTSCHE FILME",THRILLER 16 Blocks
http://xxx.xx:xx/xx/xx/632.mkv

#EXTINF:-1 tvg-id="" tvg-name="TRT HD 720" tvg-logo="http://url/trt.png" group-title="TR ULUSAL",TRT HD 720
http://xxx.xx:xx/xx/xx/218

#EXTINF:-1 tvg-id="" tvg-name="CNN TURK 1080" tvg-logo="http://url/cnnturkhd.png" group-title="TR HABER",CNN TURK 1080
http://xxx.xx:xx/xx/xx/926

#EXTINF:-1 tvg-id="" tvg-name="THRILLER 28 Weeks Later" tvg-logo="http://url/tr/Later.jpg" group-title="RUSSIA",THRILLER 28 Weeks Later
http://xxx.xx:xx/xx/xx/4805.mkv

#EXTINF:-1 tvg-id="" tvg-name="THRILLER 16 Blocks" tvg-logo="http://url/Blocks.jpg" group-title="TR DUBLAJ FILM",THRILLER 16 Blocks
http://xxx.xx:xx/xx/xx/632.mkv

#EXTINF:-1 tvg-id="" tvg-name="TRT BELGESEL HD 720" tvg-logo="http://url/trtb.png" group-title="TR BELGESEL",TRT BELGESEL 720
http://xxx.xx:xx/xx/xx/218

The result I am expecting is:

#EXTINF:-1 tvg-id="" tvg-name="TRT HD 1080" tvg-logo="http://url/tr/trt.png" group-title="TR ULUSAL",TRT HD 1080
http://xxx.xx:xx/xx/xx/123

#EXTINF:-1 tvg-id="" tvg-name="TRT HD 720" tvg-logo="http://url/trt.png" group-title="TR ULUSAL",TRT HD 720
http://xxx.xx:xx/xx/xx/218

#EXTINF:-1 tvg-id="" tvg-name="CNN TURK 1080" tvg-logo="http://url/cnnturkhd.png" group-title="TR HABER",CNN TURK 1080
http://xxx.xx:xx/xx/xx/926

#EXTINF:-1 tvg-id="" tvg-name="TRT BELGESEL 720" tvg-logo="http://url/trtb.png" group-title="TR BELGESEL",TRT BELGESEL 720
http://xxx.xx:xx/xx/xx/218

2 Answers 2

1

In sed you would used extended regular expression with option -E:

sed -E '/group-title="TR (ULUSAL|HABER|BELGESEL)"/!d;n;n'

The () around the ULUSAL|HABER|BELGESEL make sure the | only applies for the patterns inside.

A line with neither pattern (the ! behind the pattern) gets deleted. All other lines (those with the patterns) execute the n;n, so the line itself gets printed, the next one as well and the next empty line after it.

0

Assuming the file is called inputfile, and the output should be stored in outputfile:

grep --no-group-separator -A 2 -E \
"group-title=\"TR ULUSAL\"|group-title=\"TR HABER\"|group-title=\"TR BELGESEL\"" \
inputfile > outputfile

This works by using extended regular expressions (-E), and after-context (-A).

1
  • Both suggestions working great. Thank you. Commented May 11, 2019 at 17:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.