从文件中删除由特定关键字的白线分隔的一组行 [英] Remove a set of rows from a file separated by a white line having a specific key word

查看:78
本文介绍了从文件中删除由特定关键字的白线分隔的一组行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下各行的文件.我想从文件中删除一组行,如果一组行中的任何一行包含关键字SEDS2-TOP.每行的行由空白行分隔.

I have a file containing lines as given below. I want to delete a set of rows from the file, if any line from a set of rows contains key word SEDS2-TOP. Each set of rows is separated by a blank line.

0.00  600.00  2214.28   785.71 1.00000 SEDS1-BOTTOM
0.00  600.00  2214.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1500.00     0.00 1.00000 WATER-BOTTOM

0.00  600.00  3446.97  1757.08 1.00000 SEDS2-TOP
0.00  600.00  2218.64   790.51 1.00000 SEDS1-BOTTOM
0.00  600.00  2218.64   790.51 1.00000 SEDS1-TOP
0.00    0.00  600.00  1500.00  1.00000 WATER-BOTTOM

0.00  400.00  2004.28   785.71 1.00000 SEDS1-BOTTOM
0.00  300.00  2254.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1600.00     0.00 1.00000 WATER-BOTTOM

0.00  600.00  3446.97  1757.08 1.00000 SEDS2-TOP
0.00  600.00  1500.00     0.00 1.00000 WATER-BOTTOM

0.00  600.00  3446.97  1757.08 1.00000 SEDS2-TOP

示例输出文件应具有

0.00  600.00  2214.28   785.71 1.00000 SEDS1-BOTTOM
0.00  600.00  2214.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1500.00     0.00 1.00000 WATER-BOTTOM

0.00  400.00  2004.28   785.71 1.00000 SEDS1-BOTTOM
0.00  300.00  2254.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1600.00     0.00 1.00000 WATER-BOTTOM

推荐答案

您可以使用3条规则和END规则在awk中进行操作.可以这样写:

You can do it in awk using 3-rules and the END rule. It can be written as follows:

awk 'NF==0 {              # empty line
    for (i in a)          # for each line in array a
        print i           # output line (index)
    if (i in a)           # if lines exists
        print ""          # output blank line at end
    delete a              # clear a array
    del=0                 # set delete group flag 0
    next                  # get next record
}
/SEDS2-TOP/ {             # SEDS2-TOP matched in record
    del=1                 # set delete group flag 1
    delete a              # delete array a
    next                  # get next records
}
del==0 {                  # del group flag is zero
    a[$0]++               # add line as index to array a
}
END {                     # END rule - process last group of lines
    if (del==0) {         # if del group flag not set
        for (i in a)      # loop over lines in a
            print i       # output line (index)
        print ""          # with newline after
    }
}' rowsets

使用/输出示例

使用数据文件作为输入,您可以简单地选择-复制上面的脚本(并将包含行集的文件名从rowsets更改为您拥有的文件名,然后将鼠标中键粘贴到目录中的终端中)文件,例如

Using your data file as input, you can simply select-copy the script above (and change the filename containing the row-sets from rowsets to whatever you have, then middle-mouse paste into your terminal in the directory with the file, e.g.

$ awk 'NF==0 {              # empty line
>     for (i in a)          # for each line in array a
>         print i           # output line (index)
>     if (i in a)           # if lines exists
>         print ""          # output blank line at end
>     delete a              # clear a array
>     del=0                 # set delete group flag 0
>     next                  # get next record
> }
> /SEDS2-TOP/ {             # SEDS2-TOP matched in record
>     del=1                 # set delete group flag 1
>     delete a              # delete array a
>     next                  # get next records
> }
> del==0 {                  # del group flag is zero
>     a[$0]++               # add line as index to array a
> }
> END {                     # END rule - process last group of lines
>     if (del==0) {         # if del group flag not set
>         for (i in a)      # loop over lines in a
>             print i       # output line (index)
>         print ""          # with newline after
>     }
> }' rowsets
0.00  600.00  1500.00     0.00 1.00000 WATER-BOTTOM
0.00  600.00  2214.28   785.71 1.00000 SEDS1-BOTTOM
0.00  600.00  2214.28   785.71 1.00000 SEDS1-TOP

0.00  400.00  2004.28   785.71 1.00000 SEDS1-BOTTOM
0.00  300.00  2254.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1600.00     0.00 1.00000 WATER-BOTTOM

保留行顺序

如果需要保留行顺序,则可以代替使用行作为索引,而可以引入一个新的计数器变量用作与数组中的行号相对应的索引.这样一来,您就可以按原始顺序输出行,例如

If preserving the row-order is needed, then instead of using the line as the index, you can introduce a new counter variable to be used as the index that would correspond to the row number in the array. That allows you to output the rows in their original order, e.g.

awk -v ndx=1 '
NF==0 {                   # empty line
    for (i=1; i<ndx; i++) # for each line in array a
        print a[i]        # output line
    if (ndx > 1)          # if lines exists
        print ""          # output blank line at end
    delete a              # clear a array
    del=0                 # set delete group flag 0
    ndx=1                 # reset array index 1
    next                  # get next record
}
/SEDS2-TOP/ {             # SEDS2-TOP matched in record
    del=1                 # set delete group flag 1
    delete a              # delete array a
    ndx=1                 # reset array index 1
    next                  # get next records
}
del==0 {                  # del group flag is zero
    a[ndx++]=$0           # add line to array a
}
END {                     # END rule - process last group of lines
    if (del==0) {         # if del group flag not set
        for (i=1; i<ndx; i++)   # loop over lines in a
            print i       # output line (index)
        print ""          # with newline after
    }
}' rowsets

在这种情况下,您的输出将是:

In that case, your output would be:

0.00  600.00  2214.28   785.71 1.00000 SEDS1-BOTTOM
0.00  600.00  2214.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1500.00     0.00 1.00000 WATER-BOTTOM

0.00  400.00  2004.28   785.71 1.00000 SEDS1-BOTTOM
0.00  300.00  2254.28   785.71 1.00000 SEDS1-TOP
0.00  600.00  1600.00     0.00 1.00000 WATER-BOTTOM

仔细检查一下,如果还有其他问题,请告诉我.

Look things over and let me know if you have further questions.

这篇关于从文件中删除由特定关键字的白线分隔的一组行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆