真正让我感到困惑的是引号。
我有一个file.txt
像这样的行:
{"a":"town, state, country","e":["john@company.com"],"n":"john smith"}
{"a":"town, state, country","e":["zac@company.com","zacsurname@gmail.com"],"n":"zac surname"}
{"a":"town, state, country","n":"jane doe"}
Run Code Online (Sandbox Code Playgroud)
我只在寻找名字和电子邮件,并丢弃没有两者的数据。所以output.txt
上面的 3 行应该是:
john john@company.com
zac zac@company.com
zac zacsurname@gmail.com
Run Code Online (Sandbox Code Playgroud)
我试过 awk、nawk、pcregrep、sed、perl,例如:
awk -F ":" '$1 ~ /^e/ && $1 ~ /^n/ { print $1,$1 }' file.txt > output.txt
awk -F "\"e\":\"" '{ print $1}' file.txt > output.txt
nawk '/\"e\":[\"/, /\"]/' file.txt > output.txt
pcregrep -o '(?<=[\").*?(?=\"])' <<< file.txt > output.txt
Run Code Online (Sandbox Code Playgroud)
它都不起作用。感谢您的帮助。