如何将多个 sed 命令合并为一个



我无法在使用sed时一次性完成替换和删除操作,因此使用多次出现的 sed。

原始数据文件

d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB
d62150 fxn4008_d62150_bl 6227MB ^M
9465MB 9778MB 0MB

我正在使用的当前解决方案是:

sed -i -e 's/r//g; s/-/0/g; s/MB//g'  log1
sed '/^[[:space:]]*$/d' "$datafile.log1"| paste -d " " - - > log2
awk '{$1=$1}1' OFS="," "$datafile.log2" > log

Test1 out这将删除控制字符并MB,同时将-替换为所需的0

$ sed -e 's/r//g; s/-/0/g; s/MB//g' test22.log
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0
d62150 fxn3008_d62150 0
809 668 271
d62150 fxn4008_d62150_bl 6227
9465 9778 0

测试2 输出但是,我看到了另一种工作方式,如下所示,即多管道序列和用于连接行的paste命令,随后用于使其成为逗号分隔字段的awk。

$ sed -e 's/r//g; s/-/0/g; s/MB//g' test22.log | sed '/^$/d' |  paste -d " " - -|awk '{$1=$1}1' OFS=","
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0

请帮助使更容易或一个衬里。

当你使用 awk 时,你永远不需要 sed。使用任何 awk:

awk '
BEGIN { OFS="," }
!NF { next }          # Discard all empty lines
(++c) % 2 {           # Select odd-numbered of remaining lines
prev = $0         # Just save the line in a variable.
next
}
{                     # This is an even numbered non-empty line
$0 = prev " " $0  # Attach the previous line to the front of it
gsub(/-/,0)
gsub(/r|MB/,"")
$1 = $1           # Force replacement of all FS with OFS
print
}
' file

这可能对你有用(GNU sed):

sed '/^s*$/d;:a;N;s/ns*$//;ta;y/- /0,/;s/n|r|MB//g' file

删除任何空行。

建立非空行后,删除任何追加的空行,直到追加另一个非空行。

-翻译成0,将翻译成,

删除任何nrMB

相关内容

最新更新