这是我在这里发表的第一篇文章,感谢您的及时支持。
我在一个大文件上做了一些greps,并附带了如下文件。我希望在同一行的XX @开头之后创建所有行。
所以,我想要流动:
XX@DEV1>
Feb 23 07:00:03
Feb 23 07:00:05
Sent : 4608
Received : 4227
Feb 23 07:00:07
Feb 23 07:00:09
XX@DEV2>
Feb 23 07:00:32
Feb 23 07:00:34
Sent : 4608
Received : 4232
Feb 23 07:00:36
Feb 23 07:00:38
XX@DEV1>
Feb 23 08:00:03
Feb 23 08:00:06
Sent : 4608
Received : 4265
Feb 23 08:00:07
Feb 23 08:00:09
XX@DEV2>
...
成为:
XX@DEV1> Feb 23 07:00:03 Feb 23 07:00:05 Sent : 4608 Received : 4227 Feb 23 07:00:07 Feb 23 07:00:09
XX@DEV2> Feb 23 07:00:32 Feb 23 07:00:34 Sent : 4608 Received : 4232 Feb 23 07:00:36 Feb 23 07:00:38
XX@DEV1> Feb 23 08:00:03 Feb 23 08:00:06 Sent : 4608 Received : 4265 Feb 23 08:00:07 Feb 23 08:00:09
XX@DEV2> ...
答案 0 :(得分:0)
你可以很容易地用 awk 做到这一点:
awk '/^XX/{if(length(out))print out;out=$0;next}{out=out" "$0}END{print out}' yourfile
那说..如果行以“XX”开头,打印我们在变量out
中累积的任何内容,然后将当前行保存在变量out
中。如果该行是其他任何内容,请在添加空格后将其附加到变量out
。最后,打印我们在变量out
中积累的任何内容。
输出:
XX@DEV1> Feb 23 07:00:03 Feb 23 07:00:05 Sent : 4608 Received : 4227 Feb 23 07:00:07 Feb 23 07:00:09
XX@DEV2> Feb 23 07:00:32 Feb 23 07:00:34 Sent : 4608 Received : 4232 Feb 23 07:00:36 Feb 23 07:00:38
XX@DEV1> Feb 23 08:00:03 Feb 23 08:00:06 Sent : 4608 Received : 4265 Feb 23 08:00:07 Feb 23 08:00:09
答案 1 :(得分:0)
或者你可以用 bash 和我的其他答案中的 awk 一样:
#!/bin/bash
while read line
do
if [[ $line == XX* ]] ;then
echo $OUT # Output whatever we have accumulated in $OUT
OUT=$line # Zero out $OUT and restart accumulating
else
OUT="$OUT $line" # Append this line to $OUT
fi
done < yourfile
echo $OUT # Output any trailing stuff at the end
答案 2 :(得分:0)
sed -n '
# use -n to prevent sed from printing each line
# if the line read starts with XX, go (b) to :printit
/^XX/ b printit
# if it is the last line ($), also go to :printit
$ b printit
# for all other lines, append the read line to the hold space (H)
# and then go to the :end (so we read the next line)
H; b end
:printit {
# swap the hold and the pattern space
x
# replace all (s/../../g) single or multiple newlines (\n\n*)
# with a space
s/\n\n*/ /g
# print the pattern space
p
}
:end
' your_file