简体   繁体   English

在Bash中仅显示一行的一部分

[英]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. 我需要从此行中提取值-.33052537E + 03。

Using bash 使用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 : 使用awkbash

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

Using sed and bash : 使用sedbash

$ 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 : 使用GNU grepbash

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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