简体   繁体   中英

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. 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:

      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 =". For instance, for the second k, i want to save the values -0.0333 0.0000 0.0000 into output.dat file.

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?

Can you please help? Thanks in advance.

The default field separator of awk is a whitespace sequence. Since there is no space between the = and the - , awk will not split them to different fields.

If you change that to space and = then the fields you want to extract will be in $2 , $3 and $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 .

Another in awk, not tied to the fields but it searches for floats in matching records:

$ 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

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