字符串作为`paste`命令的分隔符

时间:2014-10-13 11:22:21

标签: shell unix paste

我有三个文件,其中包含以下内容:

此时就把one.txt存盘

a
b
c

two.txt

1
2
3

three.txt

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.'

也就是说,我希望字符串', '(即单引号,逗号,空格,单引号)作为三个文件之间的分隔符,并且 - 如果您想尝试一个高级答案 - 一个{ {1}}每个新行的开头和结尾。

1 个答案:

答案 0 :(得分:1)

pasteawk一起使用:

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.'