Tom*_*isi 172
如果您sed
允许通过-i
参数进行编辑:
sed -e 's/$/string after each line/' -i filename
Run Code Online (Sandbox Code Playgroud)
如果没有,你必须制作一个临时文件:
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
Run Code Online (Sandbox Code Playgroud)
Opt*_*ime 12
我更喜欢使用awk
.如果只有一列,请使用$0
,否则将其替换为最后一列.
单程,
awk '{print $0, "string to append after each line"}' file > new_file
Run Code Online (Sandbox Code Playgroud)
或这个,
awk '$0=$0"string to append after each line"' file > new_file
Run Code Online (Sandbox Code Playgroud)
欢乐的*_*aox 11
我更喜欢echo
。使用纯 bash:
cat file | while read line; do echo ${line}$string; done
Run Code Online (Sandbox Code Playgroud)
如果你有它,lam(层压板)实用程序可以做到这一点,例如:
$ lam filename -s "string after each line"
Run Code Online (Sandbox Code Playgroud)
纯POSIX 外壳和sponge
:
suffix=foobar
while read l ; do printf '%s\n' "$l" "${suffix}" ; done < file |
sponge file
Run Code Online (Sandbox Code Playgroud)xargs
和printf
:
suffix=foobar
xargs -L 1 printf "%s${suffix}\n" < file | sponge file
Run Code Online (Sandbox Code Playgroud)使用join
:
suffix=foobar
join file file -e "${suffix}" -o 1.1,2.99999 | sponge file
Run Code Online (Sandbox Code Playgroud)使用paste
, yes
, head
& 的Shell 工具wc
:
suffix=foobar
paste file <(yes "${suffix}" | head -$(wc -l < file) ) | sponge file
Run Code Online (Sandbox Code Playgroud)
请注意,在 之前paste
插入一个Tab字符$suffix
。
当然sponge
可以用临时文件替换,然后替换mv
原始文件名,就像其他一些答案一样......