在 bash 中为文本中的每一行添加前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19831827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add prefix to every line in text in bash
提问by Michael
Suppose there is a text file a.txt
e.g.
假设有一个文本文件,a.txt
例如
aaa bbb ccc ddd
I need to add a prefix (e.g. myprefix_
) to every line in the file:
我需要为myprefix_
文件中的每一行添加一个前缀(例如):
myprefix_aaa myprefix_bbb myprefix_ccc myprefix_ddd
I can do that with awk
:
我可以这样做awk
:
awk '{print "myprefix_"}' a.txt$ sed 's/^/myprefix_/' a.txt myprefix_aaa myprefix_bbb myprefix_ccc myprefix_ddd
Now I wonder if there is another way to do that in shell.
现在我想知道是否有另一种方法可以在 shell 中做到这一点。
回答by fedorqui 'SO stop harming'
With sed
:
与sed
:
$ awk '$ prefix="myprefix_"
$ awk -v prefix="$prefix" '$ nl -s "prefix_" a.txt | cut -c7-
prefix_aaa
prefix_bbb
prefix_ccc
prefix_ddd
=prefixpaste -d'' <(yes prefix_) a.txt | head -n $(wc -l a.txt)
' a.txt
myprefix_aaa
myprefix_bbb
myprefix_ccc
myprefix_ddd
="myprefix_"paste -d 'while read line
do
echo "prefix_$line"
done < a.txt
' <(yes prefix_) a.txt | head -n $(wc -l < a.txt)
' a.txt
myprefix_aaa
myprefix_bbb
myprefix_ccc
myprefix_ddd
This replaces every line beginning ^
with myprefix_
. Note that ^
is not lost, so this allows to add content to the beginning of each line.
这将替换^
以myprefix_
.开头的每一行。请注意,^
不会丢失,因此这允许将内容添加到每行的开头。
You can make your awk
's version shorter with:
您可以awk
使用以下方法缩短 's 版本:
line="12345678901234567890123456789012345678901234567890123456789012345678901234567890"
rm a.txt
for i in {1..10000} ; do
echo $line >> a.txt
done
or passing the value:
或传递值:
if [ -e b.txt ] ; then
rm b.txt
fi
echo "Bash:"
time bashtest
rm b.txt
echo
echo "Awk:"
time awktest
rm b.txt
echo
echo "Sed:"
time sedtest
It can also be done with nl
:
也可以通过以下方式完成nl
:
while read line
do
echo "prefix_$line" >> b.txt
done < a.txt
Finally: as John Zwinck explains, you can also do:
最后:正如John Zwinck 解释的那样,您还可以执行以下操作:
awk 'sed 's/^/myprefix_/' a.txt > b.txt
="myprefix_"Bash:
real 0m0.401s
user 0m0.340s
sys 0m0.048s
Awk:
real 0m0.009s
user 0m0.000s
sys 0m0.004s
Sed:
real 0m0.009s
user 0m0.000s
sys 0m0.004s
' a.txt > b.txt
on OS X:
在 OS X 上:
cat file | xargs -d "\n" -L1 echo myprefix_
回答by John Zwinck
Pure bash:
纯猛击:
##代码##回答by H?kon H?gland
For reference, regarding the speed of the awk
, sed
, and bash
solution to this question:
作为参考,关于awk
、sed
、 和bash
这个问题的解决方案的速度:
Generate a 800K input file in bash
:
在 中生成 800K 输入文件bash
:
Then consider the bash script timeIt
然后考虑bash脚本 timeIt
where bashtest
is
这里bashtest
是
awktest
is:
awktest
是:
and sedtest
is:
并且sedtest
是:
I got the following result on my machine:
我在我的机器上得到以下结果:
##代码##It seems like the bash
solution is much slower..
似乎bash
解决方案要慢得多..
回答by juj
You can also use the xargs
utility:
您还可以使用该xargs
实用程序:
The -d
option is used to allow input line with trailing blanks (related to -L
spec).
该-d
选项用于允许带有尾随空白的输入行(与-L
规范相关)。