How do I add a string after each line in a file using bash? Can it be done using the sed command, if so how?
如何使用bash在文件中的每一行之后添加字符串?如果可以,可以使用sed命令来完成吗?
4 个解决方案
#1
133
If your sed
allows in place editing via the -i
parameter:
如果您的sed允许通过-i参数进行适当的编辑:
sed -e 's/$/string after each line/' -i filename
If not, you have to make a temporary file:
如果没有,你必须做一个临时文件:
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
#2
7
If you have it, the lam (laminate) utility can do it, for example:
如果你有,林(层压)实用程序可以做它,例如:
$ lam filename -s "string after each line"
#3
6
I prefer using awk
. If there is only one column, use $0
, else replace it with the last column.
我更喜欢使用awk。如果只有一列,请使用$0,否则将其替换为最后一列。
One way,
一种方法,
awk '{print $0, "string to append after each line"}' file > new_file
or this,
或者,
awk '$0=$0"string to append after each line"' file > new_file
#4
-8
Sed is a little ugly, you could do it elegantly like so:
Sed有些丑陋,你可以这样优雅地完成:
hendry@i7 tmp$ cat foo
bar
candy
car
hendry@i7 tmp$ for i in `cat foo`; do echo ${i}bar; done
barbar
candybar
carbar
#1
133
If your sed
allows in place editing via the -i
parameter:
如果您的sed允许通过-i参数进行适当的编辑:
sed -e 's/$/string after each line/' -i filename
If not, you have to make a temporary file:
如果没有,你必须做一个临时文件:
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
#2
7
If you have it, the lam (laminate) utility can do it, for example:
如果你有,林(层压)实用程序可以做它,例如:
$ lam filename -s "string after each line"
#3
6
I prefer using awk
. If there is only one column, use $0
, else replace it with the last column.
我更喜欢使用awk。如果只有一列,请使用$0,否则将其替换为最后一列。
One way,
一种方法,
awk '{print $0, "string to append after each line"}' file > new_file
or this,
或者,
awk '$0=$0"string to append after each line"' file > new_file
#4
-8
Sed is a little ugly, you could do it elegantly like so:
Sed有些丑陋,你可以这样优雅地完成:
hendry@i7 tmp$ cat foo
bar
candy
car
hendry@i7 tmp$ for i in `cat foo`; do echo ${i}bar; done
barbar
candybar
carbar