unix获取两个引号之间出现的所有字符串的列表

时间:2014-11-19 07:06:28

标签: unix sed

我有一个像这样的文件

"Logs timestamps are good =timstamp= for new files check data=
new-char= the data is changed every =time and=

so on and i need all =data="

EOF

我想捕获所有以“=”开头并以“=”结尾的字符串,因此输出应该像'new-char''时间和''数据'我无法得到它如何 我试过跑 sed -n '/=/,/=/p'
sed -n '/\=/,/\=/p' 两者都没有给我所需的输出不确定有什么问题..请告知

2 个答案:

答案 0 :(得分:0)

试试这个:

tr -d '\n' < file | sed -r -e 's/(=[^=]+)=/\1\n/g' | sed -n 's/.*=//p'

一步一步:

<强>原始

sdlcb@Goofy-Gen:~/AMD$ cat file
"Logs timestamps are good =timstamp= for new files check data=
new-char= the data is changed every =time and=

so on and i need all =data="

<强> 1

sdlcb@Goofy-Gen:~/AMD$ tr -d '\n' < file
"Logs timestamps are good =timstamp= for new files check data=new-char= the data is changed every =time and=so on and i need all =data="sdlcb@Goofy-Gen:~/AMD$

<强> 2

sdlcb@Goofy-Gen:~/AMD$ tr -d '\n' < file | sed -r -e 's/(=[^=]+)=/\1\n/g'
"Logs timestamps are good =timstamp
 for new files check data=new-char
 the data is changed every =time and
so on and i need all =data
"sdlcb@Goofy-Gen:~/AMD$

第3:

sdlcb@Goofy-Gen:~/AMD$ tr -d '\n' < file | sed -r -e 's/(=[^=]+)=/\1\n/g' | sed -n 's/.*=//p'
timstamp
new-char
time and
data
sdlcb@Goofy-Gen:~/AMD$

答案 1 :(得分:0)

这可能适合你(GNU sed):

sed -r ':a;$!N;$!ba;s/\n//g;s/^[^=]*(=[^=]*=)/\1\n/;/^=/P;D' file

将文件粘贴到内存中,删除所有换行符,删除非必要字符串并在换行符上打印出每个必需的字符串。