-1

Situation There is a file called test that consists on the following text:

this is the first line
version=1.2.3.4
this is the third line

How can i print via bash only:

1.2.3.4

Note: I want always to print until end of line what is after "version=" not searching for 1.2.3.4 Thank you

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mr ASD
  • 103
  • 1
  • 1
  • 8

2 Answers2

1

Using GNU grep :

grep -Po '^version=\K.*'

-P enables PCRE regex, -o is used to only display what is matched rather than whole lines and the \K meta-character specifies not to display what precedes.

Using sed :

sed -n 's/^version=\(.*\)/\1/p'

-n disables auto-printing, then the substitution command will replace the "version=[...]" line by only its end through a capturing group. The substitution is only effective on the second line, which trigger the p instruction to print the (transformed) line.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • And to make sure in case the string "version=" is hidden somewhere else, it might make sense to use ^version to look for occurences at the beginning of the line – Olli Sep 28 '18 at 13:41
  • @Olli right, edited. Thanks ! – Aaron Sep 28 '18 at 13:42
1

you can use:

grep version file | cut -d\= -f2