简体   繁体   中英

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.

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

If only single line is there, then assign this line to a variable.

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

echo $var|awk '{print $3}'

If many lines are there, then insert those lines in a file, then

awk '{print $3}' file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM