bash 使用 awk 仅在分隔符后查找第一次出现的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15331259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use awk to find first occurrence only of string after a delimiter
提问by DrDavid
I have a bunch of documents that all have the line, Account number: 123456789
in various locations.
我有一堆文件,它们都Account number: 123456789
在不同的位置。
What I need to do is be able to parse through the files, and find the account number itself. So, awk
needs to look for Account number:
and return the string immediately following.
我需要做的是能够解析文件,并找到帐号本身。因此,awk
需要查找Account number:
并返回紧随其后的字符串。
For example, if it was:
例如,如果它是:
Account number: 1234567
awk
should return:
awk
应该返回:
1234567
Once it's found the first occurrence it can stop looking.
一旦找到第一次出现,它就可以停止查找。
But, I'm stumped. What's the right way to do this using awk
?
但是,我很难过。使用 什么是正确的方法awk
?
回答by Guru
One way:
单程:
awk -F: '=="Account number"{print ;exit;}' file
I assume you want to stop the moment you find the first occurence in the file. If you want to find occurrences in every line of the file, just remove the exit
.
我假设您想在找到文件中第一次出现的那一刻停止。如果您想在文件的每一行中查找出现次数,只需删除exit
.
回答by DrDavid
You can use an if
to check if $1
and $2
equal "Account" and "number:". If they do, then print $3
:
您可以使用if
检查$1
和$2
等于“帐户”和“数量”。如果他们这样做,然后打印$3
:
> awk '{if ( == "Account" && == "number:") {print ; exit;}}' input.txt
回答by fedorqui 'SO stop harming'
For such matchings I prefer using grep
with look-behind:
对于这样的匹配,我更喜欢使用grep
后视:
grep -Po '(?<=Account number: )\d+' file
or
或者
grep -Po 'Account number: \K\d+' file
This says: print whatever sequence of digits (\d+
) appearing after the string Account number:
.
这表示:打印\d+
出现在 string 之后的任何数字序列 ( ) Account number:
。
In the secondcase, \K
clears the matched string, so that it starts printing after such \K
.
在第二种情况下,\K
清除匹配的字符串,以便在此类之后开始打印\K
。
See it in action given a file file
:
在给定文件的情况下查看它file
:
Account number: 1234567
but then another Account number: 789
and that's all
Let's see how the output looks like:
让我们看看输出的样子:
$ grep -Po '(?<=Account number: )\d+' file
1234567
789
回答by malte
The accepted answer outputs a space in front of the string which forced me to use another approach:
接受的答案在字符串前面输出一个空格,这迫使我使用另一种方法:
awk '/Account number/{print ; exit}'
This solution ignores the :
separator but works like a charm and is a bit easier to remember IMO.
此解决方案忽略了:
分隔符,但效果很好,而且更容易记住 IMO。
回答by Lri
You could also use sed -n s///p
:
您还可以使用sed -n s///p
:
sed -En 's/^Account number: (.+)//p' *.txt | head -n1