如何从AWK打印后解析出一个数据 [英] How to parse out a piece of data after printing from awk

查看:129
本文介绍了如何从AWK打印后解析出一个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个文件中的一行文字如下:

say i have a line of text in a file as follows:

-config1=/path/to/config1.cfg -config2=/path/toconfig2.cfg -input=/path/to/file.txt -output=/path/to/file.out

如何我已经AWK解析出仅 -input = /路径/要/ file.txt的

现在我做:

echo $data | awk -F"-input=" '{print $2}'

和它返回到我上面整行。

and it returns to me the entire line above.

有没有办法做到这一点不使用剪切命令?这并不是说我不能使用它,但是,我试图避免大量叉/高管的。

Is there a way to do this without using the cut command? Not that i CAN'T use it but, i'm trying to avoid a lot of fork/execs.

编辑:我赶到打字这个问题。我真的很想得到我的手是输入文件的名称。在我的例子,这将是 file.txt的。我使用 -input = 作为分隔符来AWK因为起初我还以为我想 /path/to/file.txt 。这回我 /path/to/file.txt -output = /路径/要/ file.out 。我没有想。

I rushed typing this question. What i really wanted to get my hands on was the name of the input file. In my example that would be file.txt. I was using -input= as a delimeter to awk because at first i thought i wanted /path/to/file.txt. This returned to me /path/to/file.txt -output=/path/to/file.out. Which i did not want.

后来我意识到,我真的只是想/需要输入文件名。

Later I realized that i really just wanted/needed the name of the input file.

对此深感抱歉!

推荐答案

请注意,该OP通过改变尚未反映的问题意见的要求:

Note that the OP changed the requirements through comments not yet reflected in the question:


  • -input = preFIX不应该被抓获

  • 事实上,只有的文件名的路径的组件时,应捕获(仅 file.txt的中的样本数据)

  • the -input= prefix should NOT be captured
  • indeed, only the filename component of the path should be captured (only file.txt in the sample data)

如果你的 GNU 的grep 与在 -P 选项支持(启用PCRE(Perl兼容正防爆pressions)的支持),您可以用单个命令脱身

If you have GNU grep with support for the -P option (to enable PCRE (Perl-Compatible Regular Expressions) support), you can get away with a single command:

echo "$data" | grep -Po '(^| )-input=([^ /]*/)*\K[^ ]*'

请注意:假定文件路径不包含空格

Note: assumes that the file paths do not contain embedded spaces.


  • -P 激活PCRE支持

  • -o 只输出什么正则表达式匹配

  • \\氏/ code>里面的正则表达式直接丢弃匹配的一切到目前为止,导致只在文件名部分获得输出。结果
    更具体地,正则表达式的组成部分是:

    • (^ |)-input = 匹配的文字 -input = 或者在输​​入的开始如果$ p $至少1空间pceded。

    • ([^ /] * /)* 与任意数量的路径组件(不包含空格的任意长度的字符串或 / ,通过终止 /

    • \\氏/ code>,如前所述,滴到目前为止的所有匹配。

    • [^] * 则构成返回的比赛(实际上,路径的文件名部分)的非空格字符的任何序列到下一个空间或字符串的结尾。

    • -P activates PCRE support
    • -o only outputs what the regex matches
    • \K inside the regex simply drops everything matched so far, resulting only in the filename component getting output.
      More specifically, the components of the regex are:
      • (^| )-input= matches the literal -input= either at the very beginning of the input or if preceded by at least 1 space.
      • ([^ /]*/)* matches any number of path components (strings of any length not containing a space or /, terminated by /)
      • \K, as mentioned, drops everything matched so far.
      • [^ ]* then constitutes the match that is returned (effectively, the filename component of the path): any sequence of non-space characters up to the next space or end of the string.

      这篇关于如何从AWK打印后解析出一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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