異なるコンテンツセットを含む複数のファイルがありますが、異なるファイルの複数行のパターンは同じです。
入力ファイルの例: cat -n test.txt
1 adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
2 rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
3 dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj
4 a12kjsdk232kjk445kjkjlk34323lkjkjlk3422
5 98094kjhjkh23434hjhk32453242hkjhkjhkj
6 bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
7 iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450
8 5ui43u3213435
9 5io4p3i54op3i5op34i5po34i5
10 54390859043860943853kj5h34jkh534jk543
11 jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
12 4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4
13 jg;lfdjglkfjlkfjghlkfd
14 f;dgljdfl;hj;df
15 fglkdjlkjgkldfjgklfdjhklfhjdflkj
予想出力:
adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj
答え1
よく知られているsedレシピのバリエーションを使用することができます。
sed -e :a -e '$!N;/\n.*:/!s/\n/ /;ta' -e 'P;D' test.txt
つまり、前の行がある場合は、その行の後に行を追加します(改行文字をスペースに置き換えます)。いいえコロンが含まれています。だから与えられた
$ cat -n test.txt
1 adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
2 rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
3 dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj
4 a12kjsdk232kjk445kjkjlk34323lkjkjlk3422
5 98094kjhjkh23434hjhk32453242hkjhkjhkj
6 bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
7 iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450
8 5ui43u3213435
9 5io4p3i54op3i5op34i5po34i5
10 54390859043860943853kj5h34jkh534jk543
11 jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
12 4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4
13 jg;lfdjglkfjlkfjghlkfd
14 f;dgljdfl;hj;df
15 fglkdjlkjgkldfjgklfdjhklfhjdflkj
それから
$ sed -e :a -e '$!N;/\n.*:/!s/\n/ /;ta' -e 'P;D' test.txt | cat -n
1 adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
2 rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
3 dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
4 bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
5 iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
6 jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
7 4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 jg;lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj
答え2
awkを使用してください。
$ awk '/:/{if (NR>1) print r; r=$0; next} {r=r OFS $0} END{print r}' file
adjfkhjdhfkjd: dfkjfkdljgklfjgklfjgkfjghk
rtueitjrkldngfmdn: kldfjkdjgkdjgkldjgkfjhklfjkkjhkgjhklfk
dkljfkdljgkldg: fgdkjgkfdljglkfdjgkldfjgkljkldfjgkljgkflj a12kjsdk232kjk445kjkjlk34323lkjkjlk3422 98094kjhjkh23434hjhk32453242hkjhkjhkj
bncvmcnbxmbvcnmxbvnxbcnxbnxcmbvnxc: xckdfjgklfdjgklfdjglkfdjgio
iourtiourtioreutiorutoir: i3948j35hj4309457480jkh5jk450 5ui43u3213435 5io4p3i54op3i5op34i5po34i5 54390859043860943853kj5h34jkh534jk543
jkljkljkjkjkjklj: hgjjjjjjjjjjjjj
4i34935kjtkrelthrket: 4539859435943hkjhkjhk34543ll4 jg;lfdjglkfjlkfjghlkfd f;dgljdfl;hj;df fglkdjlkjgkldfjgklfdjhklfhjdflkj
答え3
使用awk
:
$ awk '{printf "%s", (($1 ~ /:$/) ? ((NR==1) ? "" : ORS) : " ")$0 } END{print ""}' file
# Or
$ awk -v ORS=' ' '{NR!=1 && gsub(/[[:alnum:]]+:/, "\n&")}1; END{printf "\n"}' file | sed 's/ $//'
@EdMortonが提案したように:
入力にprintf形式の文字(たとえば)が含まれている場合は、後者が失敗し、ORSの値を使用する前者よりも優れており、ORSに必要なハードコーディングされた値を使用する後者があるので、必ず代わりに
printf "%s", $n
実行してください。printf $n
%s
print ""
printf "\n"