'awk - regex to print first matching group
I am trying to get the first matching group based on regex, but its not printing anything after the second awk command. Not sure what I was doing wrong. Any help is greatly appreciated.
git status | awk 'NR=1' --> Limiting this to print first line.
On branch TA1692959
git status | awk 'NR=1' | awk '/^On\sbranch\s([\w]*)/{ print $1 }' --> I was trying to get the first word "TA1692959" after "On branch"this prints nothing.
Solution 1:[1]
git status |
{n,m,g}awk 'NR<--NF' FS='^On branch |[^[:alnum:]_].+$' OFS=
TA1241521
Solution 2:[2]
If you find yourself passing the data through multiple awk calls then chances are pretty good you can do the same thing with a single awk call, eg:
git status | awk 'NR==1 && /^On branch / {print $3; exit}'
TA1692959
In this case:
- there's no need for a regex; otherwise OP should update the question with additional samples showing the need for a regex
- the
exitis optional and merely allowsawkto skip processing the rest of the input stream
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RARE Kpop Manifesto |
| Solution 2 | markp-fuso |
