如何使用bash在每行中复制字符串? [英] How to duplicate strings in each line using bash?
问题描述
我有一个文本文件,每一行都包含这样的字符串:
I have a text file containing strings in each line like this:
'abc',
'dog',
'zebra',
我想像这样:
'abc', 'abc',
'dog', 'dog',
'zebra', 'zebra',
如何最好地使用bash?
How best to do it in bash?
推荐答案
您可以调用 sed
:
sed -r 's/(.*)/\1 \1/' dupcol.csv
.*
表示重复匹配一行上的任何字符.周围的()
表示将匹配项存储在第一个寄存器( \ 1
)中.然后完整匹配将输出两次.
The .*
says to match any character on a line, repeatedly. The ( )
around it says to store the match in the first register (\1
). Then the full match is output twice.
通常,我通常默认使用sed的 -r
选项制作更整洁(扩展)的正则表达式.但是,省略 -r
可使&
匹配完整模式.因此,您想保留完整匹配的这种特殊情况的简单版本是:
I just usually default to using sed’s -r
option to make for cleaner (extended) regexes. However, omitting the -r
enables &
to match the full pattern. So a simpler version of this special case where you want to keep the full match is:
sed -r 's/.*/& &/' dupcol.csv
如果要就地更改文件,请添加 -i
选项.
If you want to change the file in-place, add the -i
option.
这篇关于如何使用bash在每行中复制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!