[英]Extract negative/positive float number with awk and grep
I am searching for a way to extract negative/positive float number from a .txt file. 我正在寻找一种从.txt文件中提取负/正浮点数的方法。 So far i used the following instruction without any success:
到目前为止,我使用以下指令没有成功:
grep "k = " ./input.dat | awk '{printf "%6.4f %6.4f %6.4f\n", $3, $4, $5}' >> output.dat
A simple preview of input.dat is: input.dat的简单预览是:
k = 0.0000 0.0000 0.0000 ( 1139 PWs) bands (ev):
-5.7114 6.2665 6.2665 6.2665 8.8226 8.8226 8.8226 9.5426
13.9932 13.9932 14.1707 17.5026 17.5026 17.5026 21.6082 29.3698
29.3698 29.3698 30.4639 30.4639 31.6370 31.6370 31.6370 35.4938
35.4938 35.4938 41.1356 41.1356 41.1356 41.5299 44.9464 44.9464
46.8590 46.8590 47.5946
k =-0.0333 0.0000 0.0000 ( 1163 PWs) bands (ev):
-5.7067 6.2326 6.2452 6.2452 8.8104 8.8498 8.8498 9.5751
13.9526 13.9985 14.1923 17.5111 17.5111 17.5309 21.6438 29.2831
29.2831 29.3758 30.3994 30.4050 31.6797 31.6972 31.6972 35.5310
35.5310 35.5612 41.0551 41.0974 41.0974 41.6060 44.8492 44.9336
46.7124 46.8519 47.5656
Basically, I am looking for a way to extract all the floating number after "k =". 基本上,我正在寻找一种提取“ k =”之后的所有浮点数的方法。 For instance, for the second k, i want to save the values -0.0333 0.0000 0.0000 into output.dat file.
例如,对于第二个k,我想将值-0.0333 0.0000 0.0000保存到output.dat文件中。
So far , when I executed the previous code I cannot seem to take into account the "-" sign in front of k =-0.0333, and i don't understand why? 到目前为止,当我执行前面的代码时,我似乎无法考虑k = -0.0333前面的“-”符号,而且我不明白为什么?
Can you please help? 你能帮忙吗? Thanks in advance.
提前致谢。
The default field separator of awk is a whitespace sequence. awk的默认字段分隔符是空格序列。 Since there is no space between the
=
and the -
, awk will not split them to different fields. 由于
=
和-
之间没有空格,因此awk不会将它们拆分为不同的字段。
If you change that to space and =
then the fields you want to extract will be in $2
, $3
and $4
: 如果将其更改为space和
=
,则要提取的字段将位于$2
, $3
和$4
:
awk -F' =' '/k =/ {printf "%6.4f %6.4f %6.4f\n", $2, $3, $4}' ./input.dat
I also used a /k =/
filter in awk itself, there's no need for grep
. 我也在awk本身中使用了
/k =/
过滤器,不需要grep
。
Another in awk, not tied to the fields but it searches for floats in matching records: awk中的另一个,与字段无关,但它在匹配记录中搜索浮点数:
$ awk '$1 OFS $2 ~/k =/ { # can I do this in awk? oh wow, it works
b="" # reset buffer var
while(match($0,/-?[0-9]+\.[0-9]+/)) { # match for float
b=b (b==""?"":OFS) substr($0,RSTART,RLENGTH) # buffer it
$0=substr($0,RSTART+RLENGTH) # truncate $0
}
print b # output buffer
}' file
0.0000 0.0000 0.0000
-0.0333 0.0000 0.0000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.