新しい行\ nと今後の+記号を空白に置き換えます。
入力ファイル
ABC
+ DEF
foo
+ bar
出力は次のようになります。
ABC DEF
foo bar
ベストアンサー1
ファイルに+
次のように他のすべての行がaで始まるシフトパターンのみが含まれている場合
ABC
+ DEF
foo1
+ bar1
foo2
+ bar2
その後、使用
$ sed 'N;s/\n+ */ /' file
ABC DEF
foo1 bar1
foo2 bar2
これはただ1行を読み、次の行を追加します(使用N
)。次に、挿入された改行文字N
、プラス記号、およびその後のスペースを単一のスペースに置き換えます。
ファイルが次のようになっているとしましょう。いいえ空行(空行はない行として扱われます+
)。最初の行はプラス記号で始めることはできませんが、最後の行はプラス記号で始まります。推定プラス記号で始めます。
ABC
+ DEF
foo1
+ bar1
+ baz1
foo2
+ bar2
次のsed
スクリプトはそれを次に変換します。
ABC DEF
foo1 bar1 baz1
foo2 bar2
スクリプト:
# This is the first line
1 {
h; # Save the line in the hold space.
d; # Delete and start next cycle.
}
# This line starts with a plus sign and at least one space.
/^+ */ {
s///; # Delete the plus sign and the space(s).
H; # Append to hold space with embedded newline.
$ !d; # Delete and start next cycle (except for on the last line).
}
# This line of input starts a new set of lines.
# Output accumulated line.
x; # Swap with hold space.
y/\n/ /; # Replace all embedded newlines with spaces
# (implicit print)
次のように使用できます。
sed -f script.sed file
「一行」で:
sed -e '1{h;d;}' -e '/^+ */{s///;H;$!d;}' -e 'x;y/\n/ /' file