Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditonal file split 1

Status
Not open for further replies.

stephenk1973

Technical User
Jun 11, 2003
246
GB
I have a comma seperated file. How woudld i split the file based on a condition. eg If field 9 > field 10 go to greater.lis else lesser.lis.

Any help much appreciated.

Thanks

Stephen
 
nawk -f step.awk myFile.txt

Code:
BEGIN {
  FS=","
  less="lesser.lis"
  great="greater.lis"
}
{print $0 >> (($9 <= $10) ?  less : great) }

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Something like this ?
nawk -F',' '
{print>($9>$10?"great":"less")"er.lis"}
' /path/to/inputfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for the prompt reply.

Have tried your code and it sort of works. We don't appear to have nawk on our system just awk, is nawk specific command for numbers?

Currently the fields where the values are the same are being split into one file and all others (greater or lesser) are going into the other. Slightly confused have tried removing the "=" and all values going into the greater file. Any ideas?

Thanks

Stephen
 
pls post your sample file.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Found out why. One of the fields was a curreny and prefixed with a hidden character, only picked up on it when looked through 'vi', now awking that out as well.

Thanks for all the help.

Stephen
 
nawk stands for new awk and is used mainly on Solaris where traditional awk is very old.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top