【问题标题】:how to skip specific lines in awk and print the remaining如何跳过awk中的特定行并打印剩余的行
【发布时间】:2013-10-05 23:53:16
【问题描述】:

如何从包含 ff 行的文件中只读取行:3、9、12、15。 这个想法是每当我得到 x 和 y 时,我想打印包含 x 和 y 的行中的最后一行。 我的意思是,例如,如果我有 awk 脚本,例如: BEGIN { name = $2;价值=$3; } { if(name == x && value==y && 扫描到达第 3、9、12 和 15 行) printf("hello world") }。我可以使用什么表达式代替“扫描到达第 3、9、12 和 15 行”

1 x y
2 x y 
3 x y 
4 a d
5 e f
6 x y
7 x y
8 x y
9 x y
10 g f
11 x y
12 x y
13 p r
14 w c
15 x y
16 a z

【问题讨论】:

  • 输入中的第 2 行和第 3 行有尾随空格,真的是这样吗?

标签: awk


【解决方案1】:

像这样的?

awk 'a=="xy" && $2$3!="xy" {print b} {a=$2$3;b=$0}' file
3 x y
9 x y
12 x y
15 x y

【讨论】:

  • 这很好用,我将在 awk 脚本中使用它,而不是在命令行中使用...
  • 没问题,这在 shell 脚本中运行良好。然后,您可以将输出重定向到文件或变量。文件:awk 'a=="xy" && $2$3!="xy" {print b} {a=$2$3;b=$0}' file >result 或变量:result=$(awk 'a=="xy" && $2$3!="xy" {print b} {a=$2$3;b=$0}' file)
  • 但是有没有办法可以将其用作条件,即 if( $2 == x && $3 == y) && 扫描到达第 3、9、12 和 15 行,打印 " sth else" ,而不是行本身
  • 您可以像这样读取变量awk '$2==x && $3==y {a=$0;f=1;next} f {print a;f=0}' x="$var1" y="$var2" file 然后使用变量存储在var1var2 测试file(使用我的解决方案和sudo_Os 解决方案的组合)
  • 小心 - 例如,当 $2 为 "xy" 而 $3 为 "" 时,这将失败。
【解决方案2】:

awk 的一种方式:

$ awk '/^[0-9]+ x y$/{a=$0;f=1;next}f{print a;f=0}' file
3 x y
9 x y
12 x y
15 x y

没有awk的一种方式:

$ tac file | uniq -f1 | fgrep -w 'x y' | tac
3 x y
9 x y
12 x y
15 x y

【讨论】:

  • 嗯,awk 给了我第 1,9,12,15 行。它似乎是正确的,所以不确定什么是错的。我的awk 给出了正确的行。编辑:tac 给了我 1,3,9,12,15 所以它也有些错误。我确实使用 Ubuntu 12.04
  • 您的文件中有尾随空格。请尝试cat -E file。我对空格更严格,可能对 OP ideone.com/puI2Ss 有用,也可能没用
  • 是的,在 roberas 帖子的第 2 行和第 3 行后面有空格,删除它们并且它起作用了。由于使用了$2$3,我没有在我的解决方案中看到这一点。您的解决方案可能已编写为awk '$2$3=="xy" {a=$0;f=1;next}f{print a;f=0}',以便对空格更加健壮。
  • 我的解决方案可以通过多种方式更改以允许使用空格,但是我认为使用尾随空格并不是一个好主意,如此处所示,脚本会以神秘的方式失败。我的编辑器设置为删除不必要的空格,所以我没有发现输入不干净的事实。
  • 我使用emacs 我不确定nano 的可配置性如何。 viemacs 都非常可定制且功能强大,我的学习曲线可能很陡峭,但从长远来看,您最好还是切换一下。
【解决方案3】:

您需要在这里使用两个 while 循环,一个用于检查行,另一个用于迭代。像这样的东西。希望有帮助

字符串行 = ""; int i = 0;

    try {
        BufferedReader in = new BufferedReader(new FileReader("D:\\readline.txt"));
        while ((line = in.readLine()) != null) {
            i++;
            if (line.charAt(0) == 'x' && line.charAt(2) == 'y') {
                System.out.println("Line containg Y and Y");
                String searchline = line;
                while ((line = in.readLine()) != null) {   //Iterate untill you find the last line of X and Y
                    i++;         //To keep count of the line read
                    if (line.charAt(0) == 'x' && line.charAt(2) == 'y') {
                        searchline = line;
                        continue;
                    } else {
                        break;
                    }

                }

                System.out.println("Printing the  line ::" + (i - 1) + ":: containing X and Y::::::::" + searchline);
            }
        }

    } catch (Exception e) {
        System.out.println("Exception Caught::::");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多