我有一个格式为以下格式的文件:
create_terminal \
-name {abc} \
-port {abc} \
-layer metal1 \
-bbox {{2 0.000} {3 0.204}}
我希望输出文件如下所示:
create_terminal -name {abc} -port {abc} -layer metal1 -bbox {{2 0.000} {3 0.204}}
是否有快速的sed或awk命令来执行此操作?
由于
答案 0 :(得分:1)
使用GNU awk进行多字符RS:
$ gawk -v RS='^$' -v ORS= '{gsub(/\\\n/,"")}1' file
create_terminal -name {abc} -port {abc} -layer metal1 -bbox {{2 0.000} {3 0.204}}
其他问题:
$ awk '{rec=rec $0 RS} END{gsub(/\\\n/,"",rec); printf "%s",rec}' file
create_terminal -name {abc} -port {abc} -layer metal1 -bbox {{2 0.000} {3 0.204}}
或:
$ awk 'sub(/\\$/,""){rec=rec $0; next} {print rec $0; rec=""}' file
create_terminal -name {abc} -port {abc} -layer metal1 -bbox {{2 0.000} {3 0.204}}
以上假设您只想删除所有反斜杠 - 后续换行符。如果您想要其他内容,请编辑您的问题以澄清。
答案 1 :(得分:0)
使用awk
,你可以这样做:
awk '!/\\\s*$/{f=1} {sub(/\\\s*$/,""); a=a""$0 } f{print a; a=""; f=0}' filename
说明:
awk '
!/\\\s*$/{f=1} # set f=1 is line does not end with \ followed by whitespace(s)
{sub(/\\\s*$/,""); a=a""$0 } # substitutes the \ from the end of line with "" (nothing) and concatenates a with the current modified line.
f{print a; a=""; f=0} # prints a and reset the value of a and f. Runs only if f=1 is set
' filename
输入:
create_terminal \
-name {abc} \
-port {abc} \
-layer metal1 \
-bbox {{2 0.000} {3 0.204}}
temp_123 \
-sdg
输出:
create_terminal -name {abc} -port {abc} -layer metal1 -bbox {{2 0.000} {3 0.204}}
temp_123 -sdg
答案 2 :(得分:0)
您可以尝试tr而不是将输出传输到sed,就像这样(假设文件名是textf.txt):
cat testf.txt |tr '\n' ' ' |sed 's#\\# #g'
*注意:这适用于OS X和BSD sed
答案 3 :(得分:0)
这可能适合你(GNU sed):
sed ':a;/\\$/{$!N;ba};s/\\\n//g' file