How to grep the alphanumeric part from a text file

Viewed 107

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?

5 Answers

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

With sed

sed '/Implementation-Version: /!d;s///' file

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.

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
Related