简体   繁体   English

如果一行有特定条目,则在 output 中创建一个空行

[英]If a row has a particular entry, creating a blank line in the output

I have input.txt like so:我有这样的 input.txt:

237 @
0 2 3 4 0. ABC
ABC
DEF
@ 237
0 1 4 7 2 0.
0 3 8 9 1 0. GHI
XYZ

(a) If a row contains the symbol @ , then, in the output, I want a newline/blankline. (a) 如果一行包含符号@ ,那么,在 output 中,我想要一个换行符/空白行。

(b) If a row starts with a 0 and contains 0. then, the interval of such entries excepting the terminating 0. should be displayed. (b) 如果一行以0开头并包含0. ,则应显示除终止0.之外的此类条目的间隔。

The following script accomplishes (b)以下脚本完成(b)

awk '{
    for (i=1; i<NF; i++)
        if($i == "0")
            {arr[NR] = $i}
        else
            if ($i == "0.") 
                {break}
            else 
                {arr[NR]=arr[NR]" "$(i)}}
    ($1 == "0") {print arr[NR]}
    ' input.txt > output.txt

so that the output is:这样 output 就是:

0 2 3 4
0 1 4 7 2
0 3 8 9 1

How can (a) be accomplished so that the output is:如何实现 (a) 以使 output 为:

                // <----Starting newline
0 2 3 4

0 1 4 7 2
0 3 8 9 1

try add if ($0 ~ /@/) {print ""}尝试添加if ($0 ~ /@/) {print ""}

so所以

awk '{
    for (i=1; i<NF; i++)
        if($i == "0")
            {arr[NR] = $i}
        else
            if ($i == "0.") 
                {break}
            else 
                {arr[NR]=arr[NR]" "$(i)}
    if ($0 ~ /@/) {print ""} 
    ($1 == "0") {print arr[NR]}
    ' soinput.txt > output.txt

Is this what you're trying to do?这是你想要做的吗?

$ awk '/@/{print ""} /^0/ && sub(/ 0\..*/,"")' file

0 2 3 4

0 1 4 7 2
0 3 8 9 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM