正则表达式到 extract/output 文件中引用的字符串

Regex to extract/output quoted strings from a file

我写了一个简单的正则表达式来从文件中输出带引号的字符串

cat mobydick.txt |  while read line; do echo -n "$line "; done | grep -oP '[^"]*"\K[^"]*'

这是我目前所拥有的

例如,当我 运行 这个文件上的一行时 mobydick.txt 我得到单行输出而不是换行分隔的字符串。

有人可以帮我写剧本吗?

预期输出 --> 当上述脚本在 mobydick.txt
上 运行 "From my twenty-fifth year I date my life."
"Call me Ishmael."

以上输入文件可以从这里下载URL

使用 GNU grep(1)(其他版本的 grep(1) 没有 -P):

tr '\n' ' ' <mobydick.txt | grep -P -o '(?<=\s)"[^"]+"(?=\s)'

更准确,使用 pcregrep(1):

pcregrep -M -o '(?<=^|\s)"[^"]+"(?=$|\s)' mobydick.txt