Reputation: 23
I have multiple text files containing lines as below: """Version : 3.4.0.0 xxx xxx xxx""" I'm trying to capture 3.4.0.0 from each text file and display on the terminal. the command i've written is this :
grep -m1 'Version[\s\S]*?(\d[\s\S]+?)\n' *.txt
grep -m1 'Version[\s\S]*?(\d[\s\S]+?)\n' *.txt
I'm unable to get any results using the above code. Can someone assist me on this?
Upvotes: 2
Views: 2843
Reputation: 626826
Your pattern is a PCRE compliant regex, [\s\S]
matches any char in a PCRE pattern, but not with POSIX BRE regex flavor that you are using with your grep
since you did not use-P
option.
If you want to do it with GNU grep use
grep -oPm 1 'Version\s*:\s*\K\d+(?:\.\d+)+' *.txt
See this online demo
Details
Version
- Version
string\s*:\s*
- a colon enclosed with 0+ whitespaces \K
- match reset operator\d+
- 1+ digits and then(?:\.\d+)+
- 1 or more repetitions of .
and 1+ digits.You may do that with awk
:
awk '/^Version : [0-9]+/{print $3; exit}' *.txt
See the online awk demo:
s="Text
Version : 3.4.0.0 xxx xxx xxx
More text
Version : 5.6.0.0 xxx xxx xxx"
awk '/^Version : [0-9]+/{print $3; exit}' <<< "$s"
# => 3.4.0.0
Details
^Version : [0-9]+
finds a line that starts with Version : <1 or more digits>
{print $3; exit}
outputs the value of Field 3 and stops processing getting you just the first match.Upvotes: 4