桑达包装一些行,不是所有的

仍然古怪的文件处理(见我以前的帖子),我使用SED清理一些都摆出来,像这样:

....Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h...

我的老板要像这样的文件是标签(“\ t”的)分隔,像这样

Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h

这是我的sed代码:

sed -e '/.$/N; s/.\n/\t/'

它的工作原理,但奇怪的是,没有无处不在。 这是输出我得到

Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51 PowerOnHours = 9099h

================================================== ====================我需要更加具体。 所有的建议我产生同样的结果:它附加在一个单独的一切行。 不是我期待的。

我在寻找:

Receiver ID = ...<tab>Serial Number = ...<tab>Current temp = ...<tab>PowerOnHours = ...<tab><carriage return> Receiver ID = ...<tab>Serial Number = ...<tab>Current temp = ...<tab>PowerOnHours = ...<tab>

--------------解决方案-------------

领域不同,但在PowerOnHours结束数

awk 'ORS=/PowerOnHours/?RS:"\t"' ./infile

概念验证

$ awk 'ORS=/PowerOnHours/?RS:"\t"' receiverid
Receiver ID = 028912781755 Special Field = foo bar baz Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h
Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h

*注意特别场中的第一行


记录之间的字段数是相同的

awk 'ORS=NR%4?"\t":RS' ./infile

概念验证

$ awk 'ORS=NR%4?"\t":RS' ./infile
Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h
Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h

试试这个:

sed '/^Receiver/N;N;N;s/\n/\t/g' inputfile

说明:

  • /^Receiver/N;N;N; -每个以“接收器”开头的行被读取时,追加接下来的三行。
  • s/\n/\t/g -更换标签嵌入换行符

示例输出:

Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h

(我夸大效果的选项卡。)

您可以使用awk

$ cat file
Receiver ID = 028912781755
Serial Number = WD-WCAUH0546786
Current temp = 50C
PowerOnHours = 13166h
Receiver ID = 028920310381
Serial Number = WD-WCAUH0898333
Current temp = 51C
PowerOnHours = 9099h...

$ awk 'BEGIN{RS="Receiver";OFS="\t"}NF>1{$1=$1;print "Receiver\t"$0}' file
Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp = 50C PowerOnHours = 13166h
Receiver ID = 028920310381 Serial Number = WD-WCAUH0898333 Current temp = 51C PowerOnHours = 9099h...

sed ':a
N;/\nReceiver/{
P;D
}
s/\n/X/;ta'

由于写的,这将加入第二行的第一个,然后去三线,并加入第四个呢,等等。

sed ':b; /^$/n; N; s/.\n\(.\)/\t\1/; tb'

应该循环追加非空行。 (更正为实际捕捉运行空行。)

cat file | tr '\n' '\t' cat file | tr '\n' '\t'会工作,太

分类:SED 时间:2015-03-15 人气:0
本文关键词: SED
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.237 (s). 10 q(s)