Hi!
(I'm just a newbie in awk & bash scripts) I have a script which replaces one column from an input file with a specified one from the same file. The input and the desired output files are shown below.
~ cat input.file
random text
Fe 1.33 23.23 3.33
C 21.03 23.23 3.33
Cu 0.00 0.00 0.00
random text
%block ChemicalSpeciesLabel
1 26 Fe
3 6 C
6 29 Cu
%endblock ChemicalSpeciesLabel
random text
~ cat output.file
26 1.33 23.23 3.33
6 21.03 23.23 3.33
29 0.00 0.00 0.00
The problem is that my script for the moment produce an "incomplete" replacement (see the first column from the third line)
~ cat output.file
26 1.33 23.23 3.33
6 21.03 23.23 3.33
6u 0.00 0.00 0.00
I found on internet some pieces of code which do this with awk and adapted it for my job.
awk '{ printf("o%-2s\n",$1) }' input.file > input.file.1
sed '/%block.*ChemicalSpeciesLabel/I,/endblock.*ChemicalSpeciesLabel/I!d;/ChemicalSpeciesLabel/Id' input.file |awk '{printf("o%-3s %7s\n", $3, $2)}' > rules
for file in $(ls -1 input.file.1)
do
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a) gsub(i,a)}1' rules $file >temp.file
done
I've modified the first two awks in order to get a supplementary space character, so that the replacement done by awk, would run ok (replace "C " with 3 and "Cu" with 6), but for the moment I still get the incomplete replacement. I think the problem is on the third awk, but I really don't know how to rewrite it.
Thank you!