英文:
Match substring with regex inside file with bash
问题
我有一个包含以下内容的文本文件:
┌─ 这是文件夹描述
│
├─ ##### ─────── 品红色
│
├─ 大小 ──────── 245.7 GiB
│
├─ 网址 ──────── https://www.google.com
│
│
├─ 视频 ─────── 23.976fps
│
├─ 音频 ─────┬─ 法语
│ │
│ │
│ └─ 德语
│
├─ 字幕 ─────── 法语, 德语, 意大利语, C#
我想验证在 *音频* 和 *字幕* 之间是否有 `法语`,我不关心其余部分。
我尝试过:
grep -E '音频(.*)(法语|Franz?)(.*)字幕' test.txt
但没有匹配。我相当确定在使用 grep 时出了问题。你能指导我正确的做法吗?
英文:
I have a following text file that contains content:
┌─ This is folder description
│
├─ ##### ─────── Magenta
│
├─ Size ──────── 245.7 GiB
│
├─ URL ───────── https://www.google.com
│
│
├─ Video ─────── 23.976fps
│
├─ Audio ─────┬─ French
│ │
│ │
│ └─ German
│
├─ Subtitles ─── French, German, Italian, C#
I want to verify if there is French
between Audio and Subtitles, I don't care rest.
I tried:
grep -E 'Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))' test.txt
But no match. I'm pretty sure that is something wrong in using grep. Can lead me to correct way to do that ?
答案1
得分: 3
grep
默认每次读取一行,所以不能在单行上进行多行匹配。如果使用GNU grep来读取整个文件并使用-o
来获取输出,可以执行以下操作:
$ grep -Ezo 'Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))' test.txt
Audio ─────┬─ French
│ │
│ │
│ └─ German
│
├─ Subtitles
或者,使用awk也可以实现:
$ awk '
{ rec = rec $0 ORS }
END {
if ( match(rec,/Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))/) ) {
print substr(rec,RSTART,RLENGTH)
}
}
' test.txt
Audio ─────┬─ French
│ │
│ │
│ └─ German
│
├─ Subtitles
英文:
grep
reads 1 line at a time by default so you can't do a multi-line match on a single line. Using GNU grep for -z
(to read the whole file at one time) and -o
(as I'm guessing at what output you want):
$ grep -Ezo 'Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))' test.txt
Audio ─────┬─ French
│ │
│ │
│ └─ German
│
├─ Subtitles
Alternatively, using any awk:
$ awk '
{ rec = rec $0 ORS }
END {
if ( match(rec,/Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))/) ) {
print substr(rec,RSTART,RLENGTH)
}
}
' test.txt
Audio ─────┬─ French
│ │
│ │
│ └─ German
│
├─ Subtitles
答案2
得分: 0
如果始终有2个音频,则可以简化为:
grep Audio -A3 file.txt | grep French
添加 sed
:
sed -n '/Audio/,/Subtitles/p' file.txt | grep -v Subtitles | grep French
英文:
If there are always 2 audios than it could be simple:
grep Audio -A3 file.txt | grep French
Adding sed
:
sed -n '/Audio/,/Subtitles/p' file.txt | grep -v Subtitles | grep French
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论