Comparing floating number and replace the smaller one with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing floating number and replace the smaller one with awk?
# 1  
Old 06-09-2015
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.

Code:
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
# 2  
Old 06-09-2015
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
This User Gave Thanks to sea For This Post:
# 3  
Old 06-09-2015
Quote:
Originally Posted by sea
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
Code:
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!
# 4  
Old 06-09-2015
Try
Code:
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...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-09-2015
Quote:
Originally Posted by RudiC
Try
Code:
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...
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?
# 6  
Old 06-09-2015
In the other thread , try to replace the VIBFREQV action with
Code:
/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.

Last edited by RudiC; 06-09-2015 at 08:39 AM..
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-09-2015
Quote:
Originally Posted by RudiC
In the other thread, try to replace the VIBFREQV action with
Code:
/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.
Great! I forgot to do it in the beginning! It makes life even more simple! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing a bigger network file with a smaller one

Hello, I have two edgelists. One bigger list master.txt and a subset of that, child.txt. I want to print out all the edges in master.txt which is not there in child.txt. I have done it the Python way, but its taking way to much time as the number of edges are huge. (one thing is that A-B and B-A... (7 Replies)
Discussion started by: Sanchari
7 Replies

2. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

3. Shell Programming and Scripting

Rounding off to the nearest floating number

I have a number, which I want to convert into the nearest floating number upto two places after the decimal point. E.g. 1.2346 will become 1.23 but 1.2356 will become 1.24 . Similarly 0.009 will be 0.01 and 0.001 will be 0.00 or 0.0 (not 0, wnat to keep the decimal... (1 Reply)
Discussion started by: hbar
1 Replies

4. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

5. Shell Programming and Scripting

Number of lines smaller than specified value

Hi All, I have a problem to find number of lines per column smaller than the values given in a different file. In example, compare the 1st column of file1 with the 1st line of the file2, 2nd column of file1 with the 2nd line of the file2, etc cat file1 0.2 0.9 0.8 0.5 ... 0.6 0.5... (9 Replies)
Discussion started by: senayasma
9 Replies

6. Shell Programming and Scripting

Increment a floating number in ksh

Hi ! How to increment a varibale in ksh. #!/bin/ksh set -x RELEASE_NUM=5.2.103 VAL=0.0.1 RELEASE_NUM=`echo $RELEASE_NUM + $VAL | bc` echo $RELEASE_NUM The above code is throwing this error. + RELEASE_NUM=5.2.103 (2 Replies)
Discussion started by: dashok.83
2 Replies

7. Shell Programming and Scripting

floating point number problem

Hello folks I Hope everyone is fine. I am calculating number of bytes calculation from apache web log. awk '{ sum += $10 } END { print sum }' /var/httpd/log/mydomain.log 7.45557e+09 it show above number, what should i do it sow number like 7455, i mean if after decimal point above 5 it... (5 Replies)
Discussion started by: learnbash
5 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

using bc with floating point number in files

Hi, I' using bash and I would like to use "bc" to compute the ratio of of two numbers and assign the ratio to a variable. The numbers are in a file, e.g. 196.304492 615.348986 Any idea how to do it? N.B. I cannot change the file to have 196.304492 / 615.348986 as the file is produced by... (14 Replies)
Discussion started by: f_o_555
14 Replies

10. Shell Programming and Scripting

Replace floating-point by integer in awk

Hi, I am trying to write a script to extract multiple sets of data from a chemistry output file. The problem section is in the following format... Geometry "geometry" -> "geometry" 1 Pd 46.0000 -0.19290971 0.00535260 0.02297606 2 P ... (7 Replies)
Discussion started by: smadonald1
7 Replies
Login or Register to Ask a Question