Shell 字符串作为“粘贴”命令的分隔符
我有三个文件,内容如下: one.txtShell 字符串作为“粘贴”命令的分隔符,shell,unix,paste,Shell,Unix,Paste,我有三个文件,内容如下: one.txt a b c 1 2 3 Some text with all kinds of characters (but no single quote). More text, also with "all kinds of characters" (and no single quote either). Same as before. two.txt a b c 1 2 3 Some text with all kinds of character
a
b
c
1
2
3
Some text with all kinds of characters (but no single quote).
More text, also with "all kinds of characters" (and no single quote either).
Same as before.
two.txt
a
b
c
1
2
3
Some text with all kinds of characters (but no single quote).
More text, also with "all kinds of characters" (and no single quote either).
Same as before.
three.txt
a
b
c
1
2
3
Some text with all kinds of characters (but no single quote).
More text, also with "all kinds of characters" (and no single quote either).
Same as before.
我想将这三个文件合并为:
'a', '1', 'Some text with all kinds of characters (but no single quote).'
'b', '2', 'More text, also with "all kinds of characters" (and no single quote either).'
'c', '3', 'Same as before.'
也就是说,我希望字符串
,“
(即单引号、逗号、空格、单引号)作为三个文件之间的分隔符,并且–如果您想尝试高级答案–a”
a作为每一行的开头和结尾。使用粘贴和awk
:
paste one.txt two.txt three.txt | awk -F '\t' -v SQ="'" -v OFS=', ' '{
for (i=1; i<=NF; i++) printf "%s%s%s%s", SQ, $i, SQ, (i<NF)?OFS:ORS}'
'a', '1', 'Some text with all kinds of characters (but not single quote).'
'b', '2', 'More text, also with "all kinds of characters" (and no single quote either).'
'c', '3', 'Same as before.'
paste one.txt two.txt three.txt | awk-F'\t'-v SQ=“”“-v OFS=”,”{
对于(i=1;iYes,它确实是paste one.txt two.txt three.txt
。我刚才的文件名是1 2 3
太棒了!谢谢:-)