我有这样的文件
line1
this is line1
line2
this is line2
line3
this is line3
我想使用 awk 或 sed 删除每一行的尾随换行符,以便像这样合并它们
line1: this is line1
line2: this is line2
line3: this is line3
我如何使用 awk 或 sed
$ cat input
line1
this is line1
line2
this is line2
line3
this is line3
$ awk 'NR%2==1 {prev=$0} NR%2==0 {print prev ": " $0} END {if (NR%2==1) {print $0 ":"}}' input
line1: this is line1
line2: this is line2
line3: this is line3
$
这可能对您有用:
sed -i '$!N;s/\n/: /' file
使用sed
:
sed -n '${s/$/:/p};N;s/\n/: /p' inputFile
对于带有原始文件备份的就地编辑,
sed -n -i~ '${s/$/:/p};N;s/\n/: /p' inputFile
sed 's/^\(line.*\)/\1:/' filename | paste - -
和 Perl 类似物:
perl -ape 's/^(line.+)\n/$1: /' filename