Elisabeth
Elisabeth

Reputation: 343

Searching number in file

How to write program in awk or in something more appropriate, if it is needed, that will search and write numbers?

I have a file

0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000  -0.0000000
    0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000  -0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   1.0000000

 CHECK OF COMPUTED DEPENDENT VARIABLES FROM NORMAL EQUATIONS

   0.0921839
 -10.1319866
   3.6264119
  11.7262830
 -68.8050967
   0.4522270
   0.3373249
 -34.8902892
 -27.7340970
   1.3273557
  23.1789217
 -21.5330918
  25.0183830
 -41.1342361
  -9.4139492
  67.1457087
 -86.1216513
   0.4522270
   0.9082617
  -3.1807870
  -1.9716431
  -1.1840182
  -3.7907675
  -1.4510456
  -1.1857174
  -4.8308521
  -1.3112015
   1.1611722
  -6.1369640
  -3.5550976
  -4.7666450
  -0.6706241
  -2.2754214
  -2.9594634
  -1.9888614
  -0.9457585
  -0.9477964
 -11.1808337
  -9.6823234
  -6.0165193
  -4.6256076
 SUM OF ABSOLUTE VALUES OF CHECKS IS 0.188070D-10
                      Input-Output in F Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
 9      0     43.8999000000      2.4976090669     46.3975090669      0.3593736560
10      0      0.0883000000     -0.0008250571      0.0874749429      0.0006398321
11      0      2.5816650000     -0.0033525536      2.5783124464      0.0109309501
12      0      1.0000000000      0.0481656121      1.0481656121      0.0288356492
13      0      1.0000000000     -3.1951648165     -2.1951648165      2.0603892225
14      0      0.0000000000     -0.0002127349     -0.0002127349      0.0017416849
15      0      0.2175000000      0.0015097548      0.2190097548      0.0015804101
16      0     80.4198910000     -4.3952312032     76.0246597968      0.1589276670
17      0      1.0000000000     -2.5673410799     -1.5673410799      0.0871801072
18      0      1.0000000000   -205.4932338114   -204.4932338114     14.4204106262
19      0      3.0000000000     -0.2245759531      2.7754240469      0.0822226758
20      0      1.9424000000      0.1257878585      2.0681878585      0.0430855010
21      0      1.0000000000    -18.7946060528    -17.7946060528      2.0771181978
22      0      1.0000000000     -2.8342434886     -1.8342434886      0.2995629339
23      0      3.5047300000     -0.3237559753      3.1809740247      0.0128735907
24      0      5.5942300000      0.6858315083      6.2800615083      0.0486118539
25      0      0.3827000000     -0.0227502151      0.3599497849      0.0041910161

And I would like to get

46.397509
0.359373
0.087474
0.000639
....

So how to write something like if row after string Input-Output in F Format begin with 9 then write $5 in format %2.6f (6 decimal places) and to next row write $6 in format %2.6f (6 decimal places)

There is a similar table in the input file, so I need to search only the first row that start with this number.

Upvotes: 2

Views: 96

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133620

EDIT2: After chatting with OP got to know that we need to print 5th and 6th fields from Input_file after line matches Input-Output in F Format in Input_file as follows.

awk '/Input-Output in F Format/{flag=1;next} flag && ($0 ~ /^[0-9]/ ||  $0 ~ /^ [0-9]+/){printf("%.06f\n%.06f\n",$5,$6)}' Input_file


Is this you want, not clear but giving it a try. Here $(NF-1) means 2nd last and $NF means last field of current line. So you could change field numbers as per your need too.

awk 'FNR>4{printf("%.06f\n%.06f\n",$(NF-1),$NF)}' Input_file

Output will be as follows.

46.397509
0.359374
0.087475
0.000640
......... so on.....

EDIT: Since OP's Input_file could have TAB delimited data so suggesting changing awk to awk -F'\t' to OP here.

Also in case you want to see column numbers in a line then you could run following.

awk '{for(i=1;i<=NF;i++){print "field_number",i,"field value",$i}}' Input_file

FINAL SOLUTION

awk '/^$/{flag=""} /Input-Output in F Format/{flag=1;next} flag && ($0 ~ /^[0-9]/ || $0 ~ /^ [0-9]+/) && ($0 !~ /[2][89]/ && $0 !~ /[3][01]/){printf("%.06f\n%.06f\n",$5,$6)}' Input_file

OR adding a non-one liner form of above solution now:

awk '
/^$/{
  flag=""
}
/Input-Output in F Format/{
  flag=1
  next
}
flag && ($0 ~ /^[0-9]/ || $0 ~ /^ [0-9]+/) && ($0 !~ /[2][89]/ && $0 !~ /[3][01]/){
  printf("%.06f\n%.06f\n",$5,$6)
}'   Input_file

Upvotes: 2

Related Questions