41

I have a bunch of documents that all have the line, Account number: 123456789 in various locations.

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.

For example, if it was:

Account number: 1234567

awk should return:

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?

5 Answers 5

61

One way:

awk -F: '$1=="Account number"{print $2;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 .

9

You can use an if to check if $1 and $2 equal "Account" and "number:". If they do, then print $3:

> awk '{if ($1 == "Account" && $2 == "number:") {print $3; exit;}}' input.txt
3
  • Exactly how would I do that?
    – DrDavid
    Mar 11, 2013 at 4:39
  • 1
    Why the extra {}? Why not just awk '$1 == "Account" && $2 == "number:" { print $3; exit }' input.txt?
    – tianon
    Feb 6, 2014 at 0:48
  • 2
    @tianon Actually you can do this: awk '/^Account number: / { print $3; exit }' FILE
    – Six
    Mar 30, 2015 at 16:51
7

The accepted answer outputs a space in front of the string which forced me to use another approach:

awk '/Account number/{print $3; exit}'

This solution ignores the : separator but works like a charm and is a bit easier to remember IMO.

3

For such matchings I prefer using grep with look-behind:

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:.

In the secondcase, \K clears the matched string, so that it starts printing after such \K.


See it in action given a 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
2

You could also use sed -n s///p:

sed -En 's/^Account number: (.+)/\1/p' *.txt | head -n1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.