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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and copy files with field lower than a value, awk?
# 1  
Old 01-10-2015
Find and copy files with field lower than a value, awk?

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

Code:
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
# 2  
Old 01-10-2015
Quote:
(additionally how can I add a second limittation lets say rms lower than 0.1 and GAP<100)
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?
This User Gave Thanks to ongoto For This Post:
# 3  
Old 01-10-2015
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.

Code:
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-10-2015
In addition to what ongoto and RavinderSingh13 asked, Is RMS also always in a fixed column?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-10-2015
Maybe this will help, could be done more elegantly of course..

Code:
 
 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

This User Gave Thanks to senhia83 For This Post:
# 6  
Old 01-10-2015
thank you all for youl help! I always get the best answers from this forum!

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.
Code:
awk '($4=="GAP" && $5<100 && $7 == "RMS" && $8 < 0.1)  {print "cp " FILENAME " temp/" }'  *  | sh

Thank you all for your time and help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk command to copy field to bottom records

Hi All, I have a below input, I want to copy the Job_name and Created_by field information to other bottom records as shown in below Output Job_name Created_by Modified_on Modified_by CGI_ACLMIB n38504 2014-05-07 20:40:48 n38504 2014-05-07 20:40:57 n38504 2014-05-08 20:40:57 n48504... (1 Reply)
Discussion started by: somu_june
1 Replies

2. Shell Programming and Scripting

Find and delete part of field with awk or sed

I've got a file that looks like this (the whitespace between commas is intentional): 123456789,12,JOHN H DOE ,DOE/JOHN H ,,,DOE/JOHN H ,,,,,123 FAKE STREET ,SPRINGFIELD,XX, I want to strip just the first name out of the third field so it reads "JOHN,". So far I... (6 Replies)
Discussion started by: Scottie1954
6 Replies

3. Shell Programming and Scripting

Find and copy these files to particular directory

RedHat Enterprise Linux 5.4 I have some files with the extension .cdp in several directories in various mountpoints(filesystems) . I would like to find and copy all these files into a single directory /u03/diagnore/data. How can I do this ? (3 Replies)
Discussion started by: kraljic
3 Replies

4. UNIX for Dummies Questions & Answers

How to use wild cards to find files beginning with upper and lower case

Im trying to use wild cards to find files that start with either an upper or lower case letter e.g. list files that beginning with b or B, i also want to sort them by the time they were last modified. e.g latest file created first. At the moment i have the following code that ls -d... (3 Replies)
Discussion started by: parker4001
3 Replies

5. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. Shell Programming and Scripting

How to find files and then copy them to another

I must write any shell script. I want find files which have .txt extension and then copy them to other, whithout this extension, for example: I found linux.out.txt file, and now it must be copy to new, linux.out. So: linux.out.txt -> linux.out ubuntu.config.txt -> ubuntu.config ... (4 Replies)
Discussion started by: piespluto
4 Replies

7. Shell Programming and Scripting

Awk to find duplicates in 2nd field

I want to find duplicates in file on 2nd field i wrote this code: nawk '{a++} END{for i in a {if (a>1) print}}' temp Could not find whats wrong with this. Appreciate help (5 Replies)
Discussion started by: pinnacle
5 Replies

8. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

9. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies

10. Shell Programming and Scripting

find files and copy into a directory

hi all, can u please help me in finding all ksh file in directory and including all subdirectories and then copy those files into another directory. thanks in advance -bali (4 Replies)
Discussion started by: balireddy_77
4 Replies
Login or Register to Ask a Question