For flexibility, here’s an approach which accepts the number of the entry to be moved up as a command line parameter:
#! perl use strict; use warnings; my $target = $ARGV[0] - 1; my $text = <<'LIST'; #EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=80000 80.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=400000 400.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=700000 700.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1500000 1500.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2500000 2500.m3u8 LIST my $entry = qr{ \#EXT-X-STREAM-INF:PROGRAM-ID= \d+ ,BANDWIDTH= \d+ \s+ \d+\.m3u8 \s+ }xms; if ($text =~ m{ ( \A \#EXTM3U \s+ ) #1 ( (?: $entry){$target} ) #2 ( (?: $entry) ) #3 ( (?: $entry)* ) #4 }xms) { print "$1$3$2$4"; } else { print "No match found\n"; }
Output:
12:57 >perl 710_SoPW.pl 3 #EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=700000 700.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=80000 80.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=400000 400.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1500000 1500.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2500000 2500.m3u8 12:57 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Multiline regex for moving lines?
by Athanasius
in thread Multiline regex for moving lines?
by bowei_99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |