如何删除以特定字符串开头的行

如何删除以特定字符串开头的行

有没有办法删除以某些字符串开头的行。

我有这个 youtube-dl 代码

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E

其结果如下

[youtube:playlist] PL1C815DB73EC2678E: Downloading webpage
[download] Downloading playlist: Less than 1 minute
[youtube:playlist] playlist Less than 1 minute: Collected 4 video ids (downloading 4 of them)
[download] Downloading video 1 of 4
[youtube] KNLwsqzFfNg: Downloading webpage
[youtube] KNLwsqzFfNg: Extracting video information
[youtube] KNLwsqzFfNg: Downloading DASH manifest
[download] Destination: _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a

[download]   0.4% of 231.51KiB at  6.10KiB/s ETA 00:30
[download]   1.1% of 231.51KiB at 27.07KiB/s ETA 00:10
[download]   4.0% of 231.51KiB at 19.24KiB/s ETA 00:04
[download]   6.5% of 231.51KiB at 75.06KiB/s ETA 00:03
[download]  13.4% of 231.51KiB at 98.22KiB/s ETA 00:03
[download]  28.7% of 231.51KiB at 81.40KiB/s ETA 00:02
[download]  61.7% of 231.51KiB at 91.56KiB/s ETA 00:01
[download]  86.2% of 231.51KiB at 82.96KiB/s ETA 00:00
[download] 100.0% of 231.51KiB at 73.21KiB/s ETA 00:00
[download] 100% of 231.51KiB in 00:02
[ffmpeg] Correcting container in "_1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a"
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
[avconv] Destination: _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.mp3
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
Deleting original file _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a (pass -k to keep)
[download] Downloading video 2 of 4
[youtube] wTvXkMpJflk: Downloading webpage
[youtube] wTvXkMpJflk: Extracting video information
[youtube] wTvXkMpJflk: Downloading DASH manifest

我想删除所有以[youtube],开头的行[ffmpeg] ,然后 [avconv]得到如下结果

[youtube:playlist] PL1C815DB73EC2678E: Downloading webpage
[download] Downloading playlist: Less than 1 minute
[youtube:playlist] playlist Less than 1 minute: Collected 4 video ids (downloading 4 of them)
[download] Downloading video 1 of 4
[download] Destination: _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a

[download]   0.4% of 231.51KiB at  6.10KiB/s ETA 00:30
[download]   1.1% of 231.51KiB at 27.07KiB/s ETA 00:10
[download]   4.0% of 231.51KiB at 19.24KiB/s ETA 00:04
[download]   6.5% of 231.51KiB at 75.06KiB/s ETA 00:03
[download]  13.4% of 231.51KiB at 98.22KiB/s ETA 00:03
[download]  28.7% of 231.51KiB at 81.40KiB/s ETA 00:02
[download]  61.7% of 231.51KiB at 91.56KiB/s ETA 00:01
[download]  86.2% of 231.51KiB at 82.96KiB/s ETA 00:00
[download] 100.0% of 231.51KiB at 73.21KiB/s ETA 00:00
[download] 100% of 231.51KiB in 00:02
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
WARNING: Your copy of avconv is outdated, update avconv to version 10-0 or newer if you encounter any errors.
Deleting original file _1 min. - Amendes pour les particules du LHC-KNLwsqzFfNg.m4a (pass -k to keep)
[download] Downloading video 2 of 4
etc..
etc..
.
.

我试过这个方法,但它显示错误,它只应该删除[youtube]

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E | sed '^/[youtube]/ d' 

答案1

sed -i '/\[youtube\]/d' /path/to/file

将删除包含“[youtube]”的行。

你可以用一个命令组合多种模式,例如

sed -i '/\[youtube\]\|\[ffmpeg\]\|\[avconv\]/d' /path/to/file

或者直接从你的命令

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 \
 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E | 
    sed '/\[youtube\]\|\[ffmpeg\]\|\[avconv\]/d' > output.txt

这会将结果写入文件 output.txt。

如果您想要删除的行不仅包含[youtube],而且以 开头[youtube],则添加^到模式中,例如sed '/^\[youtube\]/d'

但对于你来说这并不重要。

答案2

我建议grep -vE像这样使用:

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E | grep -vE '^\[(youtube|ffmpeg|avconv)\]'

man grep

-v, --invert-match
              Invert the sense of matching, to select non-matching lines.  (-v
              is specified by POSIX.)



-E, --extended-regexp
              Interpret  PATTERN  as  an extended regular expression (ERE, see
              below).  (-E is specified by POSIX.)

-E 标志用于避免使用斜杠转义方括号。如果没有 -E 标志,则必须使用反斜杠转义方括号,如下所示grep -vE '\[youtube\]\|\[ffmpeg\]\|\[avconv\]' 编辑:

由于您已提出要求awk,这里有一个awk

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E | awk '{if ($0~/^\[youtube\]/||/^\[ffmpeg\]/||/^\[avconv\]/||/^WARNING/) next;print}'

答案3

使用grep -v方法如下:

youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E | grep -v '^[youtube]' | grep -v '^[ffmpeg]' | grep -v '^[avconv]'

答案4

这是一个类似的问题,答案很简单

https://askubuntu.com/questions/1493280/remove-lines-from-a-txt-file-that-start-with-a-special-string?noredirect=1#comment2616486_1493280 

相关内容