如何围绕每个裸字获取单引号
$ cat /tmp/foojoo
my $header = "Symbol
Exchange
Expiry
oCQJoinEdgeMult
oCQEdgeMult
oCQKeepEdgeMult
oTOEdgeMult
oTOMinEdgeMult
但似乎无法通过单词获得单引号; 像这样这就是我想要的。
my $header = " 'Symbol'
'Exchange'
'Expiry'
'oCQJoinEdgeMult'
'oCQEdgeMult'
'oCQKeepEdgeMult'
'oTOEdgeMult'
'oTOMinEdgeMult'
我试过了:
cat /tmp/foojoo | sed -e 's/^$/'\w'/g'
cat /tmp/foojoo | sed -e 's/^$/' '/g'
cat /tmp/foojoo | sed -e 's/\w/'\w'/g'
cat /tmp/foojoo | awk '{for(i=1;i<=NF;i++)printf("\"%s\" ",$i);printf("\n");}' | tr '"' '\''
答案 0 :(得分:1)
通过perl,
$ perl -pe "s/(\w+)$/'\1'/g" file
my $header = "'Symbol'
'Exchange'
'Expiry'
'oCQJoinEdgeMult'
'oCQEdgeMult'
'oCQKeepEdgeMult'
'oTOEdgeMult'
'oTOMinEdgeMult'
$ perl -pe "s/(\w+)$/'\1'/g" file | sed "s/\"'/\" '/g"
my $header = " 'Symbol'
'Exchange'
'Expiry'
'oCQJoinEdgeMult'
'oCQEdgeMult'
'oCQKeepEdgeMult'
'oTOEdgeMult'
'oTOMinEdgeMult'
答案 1 :(得分:1)
看起来你的字符串只是一个单词列表。怎么样这样做:
my @words = qw(Symbol Exchange Expiry oCQJoinEdgeMult oCQEdgeMult oCQKeepEdgeMult oTOEdgeMult oTOMinEdgeMult);
for my $word (@words)
{
$word = "'$word'";
}
答案 2 :(得分:1)
使用awk
:
awk -v s="'" '
{
if(NR==1) {
sub("\"","\""s,$NF)
$NF=$NF s
} else {
$0=s $0 s
}
}1' file
输出:
my $header = "'Symbol'
'Exchange'
'Expiry'
'oCQJoinEdgeMult'
'oCQEdgeMult'
'oCQKeepEdgeMult'
'oTOEdgeMult'
'oTOMinEdgeMult'
答案 3 :(得分:1)
这可能适合你(GNU sed):
sed "s/\w\+$/'&'/" file