Find and copy files with field lower than a value, awk?

Hi all!
I have 10.000 files having generally this format:

text text text
text num text num text num
text text text GAP number text text
text num text num text num RMS num
text num text num text num
...

what I want is to copy the files if the GAP number is lower than a value e.g. <100

the word gap is always the 12th field and the number is the 13nth

(additionally how can I add a second limittation lets say rms lower than 0.1 and GAP<100)

Thank you in advance for your help and time

Will GAP number and RMS num ever be in the same line?
or is the test to be for their one time occurance in the whole file?

1 Like

Hello phaethon,

You should us some input and more details like is RMS and GAP will be on same line. After making an assumption like you have
RMS as 12th field only and is on a different line with a single occurrence in whole file.

awk '($4 == "GAP" && $5 < 100) {A=1} A && ($7 == "RMS" && $8 < 0.1) {print "cp " FILENAME " /tmp/" FILENAME"_"++i}'   Input_file

If you are satisfied with above command's output, you can use full command to copy the files as follows.

awk '($4 == "GAP" && $5 < 100) {A=1} A && ($7 == "RMS" && $8 < 0.1) {print "cp " FILENAME " /tmp/" FILENAME"_"++i}'  Input_file | sh

You can pass al file names to this command either by for loop or find command, depends on your conditions/requirements completely.
Let me know if you have any queries on same.

Thanks,
R. Singh

1 Like

In addition to what ongoto and RavinderSingh13 asked, Is RMS also always in a fixed column?

1 Like

Maybe this will help, could be done more elegantly of course..

 
 for file in *
do
 gap=$( awk '/Gap/' "$file" | sed -e 's/.*Gap//;' | awk '{print $1}'  );
rms=$( awk '/RMS/' "$file" | sed -e 's/.*RMS//;' | awk '{print $1}'  );
 
if awk "BEGIN {exit $gap < 100 ? 0 : 1}"
then
 if awk "BEGIN {exit $rms < 0.1 ? 0 : 1}"
then
cp "$file" cp"$file"
fi
fi
 done
 
1 Like

There is only one occurrence of both RMS and GAP in the file and both in fixed field locations.

Spacial thanks to RavinderSingh13
Following his advise I did something slightly different since I didn't want to modify the name of the file in the end.

awk '($4=="GAP" && $5<100 && $7 == "RMS" && $8 < 0.1)  {print "cp " FILENAME " temp/" }'  *  | sh

Thank you all for your time and help