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