4

我有这样的文件

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

4

4 回答 4

3
$ 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
$ 
于 2012-06-02T01:58:43.130 回答
2

这可能对您有用:

sed -i '$!N;s/\n/: /' file
于 2012-06-02T07:31:49.193 回答
2

使用sed

sed -n '${s/$/:/p};N;s/\n/: /p' inputFile

对于带有原始文件备份的就地编辑,

sed -n -i~ '${s/$/:/p};N;s/\n/: /p' inputFile
于 2012-06-02T02:47:00.297 回答
1
sed 's/^\(line.*\)/\1:/' filename | paste - -

和 Perl 类似物:

perl -ape 's/^(line.+)\n/$1: /' filename
于 2012-06-02T02:34:08.890 回答