1

I have a file with following content:

some text
some text
Implementation-Version: 1.1.1
some text

When I use:

grep Implementation-Version filename

so my output is Implementation-Version: 1.1.1

How can I get only the Version number 1.1.1?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
ng-User
  • 243
  • 1
  • 6
  • 18

5 Answers5

2

You can use awk like that:

grep Implementation-Version filename | awk  -v FS=': ' '{print $NF}'

Or, without using grep at all:

awk '$1=="Implementation-Version:" {print $2}' filename
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • Thanks, that works. But if I want to use the version number for other purposes, I get a "?" Character at the end, e.g. with touch $ (grep ..) I get 1.1.1?_20210413 instead of 1.1.1_20210413 as output. Doesn't grep cut the version number exactly? – ng-User Apr 13 '21 at 20:38
  • `touch "$(grep Implementation-Version filename | awk -v FS=': ' '{print $NF}')"` creates `1.1.1`, there is no `?` in its name. – Arkadiusz Drabczyk Apr 13 '21 at 20:41
  • strange. For me it creates a "?" at the end. If I auto-complete the file with TAB on console, the file is even called 1.1.1^M. I do not understand that. – ng-User Apr 13 '21 at 21:25
1

With GNU grep:

grep -Po 'Implementation-Version: \K.*' file

or

grep -Po '(?<=Implementation-Version: ).*' file

Output:

1.1.1

See: man grep and The Stack Overflow Regular Expressions FAQ

Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

With sed

sed '/Implementation-Version: /!d;s///' file
ctac_
  • 2,413
  • 2
  • 7
  • 17
  • that works, but I when use it in bash script like version=$(sed '/Implementation-Version: /!d;s///' file), cp myfile.txt myfile_${version}_$(date ...).txt I'm getting as result myfile_1.1.1?_20210417. Note the "?" at end of version. Do you know why? – ng-User Apr 17 '21 at 16:00
  • @ng-User A lot of time the system use the question mark when it find a char that it know nothing about. Try to see with cat -A file if there is a char like that at the end of the version line. – ctac_ Apr 17 '21 at 17:14
1

Another sed variation would be:

sed -n 's/^Implementation-Version:\s*//p' file

Where the normal substitution form 's/find/replace/' is used along with the -n option to suppress normal printing of pattern space and p following the substitution to output after a successful substitution.

The find part anchor "Implementation-Version:" to the beginning of the line with the circumflex character '^'. The \s+ matches zero-or-more whitespace characters that follow. Then on a successful match, the content is replaced with nothing, effectively trimming the beginning of the line though the final space leaving your desired output.

Example Use/Output

With your example text in the file named content, you would have:

$ sed -n 's/^Implementation-Version:\s*//p' content
1.1.1

Look things over and let me know if you have questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

With default grep and cut:

grep Implementation-Version file | cut -d " " -f2

Or with a line of awk:

awk '{ if ($1 == "Implementation-Version:") print $2 }' file

Output:

1.1.1
Synthase
  • 5,849
  • 2
  • 12
  • 34