Comparing floating number and replace the smaller one with awk?

Hi guys,

I tried to write a script for replacing some number in a bunch of files. However, I failed because my limited bash scripting knowledge.
Here I will explain the expected function:

I have many data files need to be processed. Here shows one part of a file as an example: a list with floating numbers and the first line shows the total number (line 28) of the following floating numbers. The floating numbers are always start from line 29.

Vibrational Frequencies (first line: number; then freqs. in cm-1)
  45
  -5.4952
  16.4194
  1.7444
  338.9944
  412.1778
  489.8751
  600.0832
  808.3847
  832.5173
  1005.0037
  1052.8224
  1389.7125
  1449.3628
  2079.3164
  3207.1898
  -4.3289
  8.7188
  314.0876
  366.5319
  418.7471
  514.9822
  601.0465
  820.4131
  897.3149
  1011.9912
  1131.1495
  1394.6233
  1996.4284
  3194.1822
  3212.9026
  6.0751
  98.1509
  319.9804
  410.7211
  449.9008
  568.3643
  797.9918
  831.0615
  903.9862
  1050.9214
  1257.6633
  1444.1448
  2006.4230
  3194.9175
  3220.1831

Function one: comparing each of these floating numbers with 0; if smaller than 0; just replace this negative number with its absolute value or simply by removing the negative sign; this with generate new file 1;
Function two: reading or setting a threshold value, for instance 10.0; compare each of these floating numbers including the original negative numbers with this threshold value 10.0; if smaller than 10.0; just replace this number with 10.0; this with generate new file 2;

Thank you very much for your kind efforts and for your time!
Zhen

Hi Zhen

You know the drill, what have you tried yourself?
You could:
1) use read line, 'ignore' the first line, and then do checks on every later line (num)
2) use some awk

Thank you

1 Like
FREQ0=$1

for i in $(ls *.log | sed "s/.log//g");do

NUM1=$(sed -n 28p ${i}.in)
NUM2=$((NUM1 + 28))

for ((j=29;j<=$NUM2;j++)); do
FREQ1=$(sed -n ${j}p ${i}.in)

awk -v LINE="$j" -v n1="${FREQ1}" -v n2="${FREQ0}"  'BEGIN{
 if (n1<n2) printf ("%s < %s\n", n1, n2);

 else
 printf ("%s >= %s\n", n1, n2)}'

done
done

Thank you very much for your kind replay;
I successfully compared these values and bash returned with the right comparison results. The problem is I cannot do the replacement inside awk. Do you have some ideas for this?

many thanks!

Try

awk '{print $1<10?10:$1 >"file2"; sub(/-/,_); print >"file1"}' file

---------- Post updated at 13:07 ---------- Previous update was at 13:04 ----------

If I remember correctly, you had another thread that produced above numbers. This evaluation could be made part of that program as well...

1 Like

Hi RudiC,

Yes, indeed. I should post this add-up function to that post instead to start a new thread. Can I merged these two threads at this moment?
You are really a great bash programmer!! I tried yesterday and this morning to do this substitution, but failed. You made it true in one LINE!
These wavenumbers are the last parts of the newly generated file. However, some negative values are meaningless and need to be replaced with positive value of its absolute value or some new values as assigned by me. The substitution should only do the replacement with the line 29 and 29+ 45 (total number of these following floating numbers); ortherwise, the substitution would destroy other values inside the input file. Do you have some suggestions on this?

In the other thread , try to replace the VIBFREQV action with

/VIBFREQV/              {print 3 * FC
                         for (i=3; i<=5; i++)
                                for (j=1; j<=FC; j++)
                                        {T=FR[j,i] ""
                                         print T
                                         print T<10?10:T > "file2"
                                         sub (/-/, _, T); print T > "file1"
                                        }
                         next}

This is untested; please report back the results.

1 Like

Great! I forgot to do it in the beginning! It makes life even more simple! Thanks!

Make print T<10?10:T > "file2" --> print T+0<10?10:T > "file2"

1 Like