Shivam Saxena Shivam Saxena - 5 months ago 6
Linux Question

Displaying only a part of a line in Bash

I have a line of data. It contains both words and numbers.It is

15 F= -.33052537E+03 E0= -.33051414E+03 d E=.720942E-05 mag=24.6037:3


I need to extract the value -.33052537E+03 from this line. Can anyone please help me out.

Answer

Using bash

$ read one two three rest <<<'   15 F= -.33052537E+03 E0= -.33051414E+03 d E=.720942E-05 mag=24.6037:3'
$ echo "$three"
-.33052537E+03

Using awk and bash:

$ awk '{print $3}' <<<'   15 F= -.33052537E+03 E0= -.33051414E+03 d E=.720942E-05 mag=24.6037:3'
-.33052537E+03

Using sed and bash:

$ sed 's/.*F= //; s/ E0=.*//' <<<'   15 F= -.33052537E+03 E0= -.33051414E+03 d E=.720942E-05 mag=24.6037:3'
-.33052537E+03

Using GNU grep and bash:

$ grep -oP '(?<=F= ).*(?= E0=)' <<<'   15 F= -.33052537E+03 E0= -.33051414E+03 d E=.720942E-05 mag=24.6037:3'
-.33052537E+03