Splitting file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting file using awk
# 1  
Old 09-11-2012
Splitting file using awk

I have file with below content
Code:
FG1620000|20000
FG1623000|23000
FG1625000|25000
FG1643894|43894
FG1643895|43895
FG1643896|43896
FG1643897|43897
FG1643898|43898

My aim is to split the above file into two files based on the value in the second field.
If the value in second field is <=30000, the field one should written into a separate file "high.txt"

"high.txt"
Code:
FG1643894|43894
FG1643895|43895
FG1643896|43896
FG1643897|43897
FG1643898|43898

"low.txt"
Code:
FG1620000|20000
FG1623000|23000
FG1625000|25000

My code is
Code:
cat file| awk -F'|' '{ if ($1 <= "20000") {print $1 >> "high.txt"} else {print $1 >> "low.txt"} }'

but the entire content of the file gets printed to "low.txt"

Where am I going wrong?
# 2  
Old 09-11-2012
That's useless use of cat.

And you are comparing $1, you should compare $2Smilie

try this..
Code:
awk -F'|' '{ if ($2 >= 30000) {print  >> "high.txt"} else {print  >> "low.txt"} }' file


Last edited by pamu; 09-11-2012 at 07:46 AM.. Reason: changed..
# 3  
Old 09-11-2012
Beware, some awk implementations will produce unexpected results with an expression such as $2 <= "20000". Although the comparison is intended to be a numeric one, some awks (gawk included) will treat that as string comparison and return true if $2 is (say) 100000.
Code:
gawk 'BEGIN{a=100000;if(a<="20000") print "string comparison"}'
string comparison

It's better to force numeric comparison and/or drop the quotes:
Code:
a<=20000

or
Code:
a+0<=20000+0

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk solution for Splitting a file.

Hi I have a csv file with as below sdg-catalog-00000001 sdg-sku-00000317 sdg-sku-00000318 sdg-sku-00000319 sdg-sku-00000320 sdg-catalog-00000002 sdg-sku-00000321 sdg-sku-00000322 sdg-sku-00000323 sdg-sku-00000324 sdg-sku-00000325 sdg-catalog-00000003 sdg-sku-00000326... (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

4. Shell Programming and Scripting

Splitting the file using awk

Hi, I have a requirement in which I am going to receive one file and should be splitted to 9 different files based on one distinguisher called TYPE. I heard that this can be done using awk or sed. Can any one advise regardint the logic and simpler way other than using awk or sed is also... (15 Replies)
Discussion started by: sagar.cumar
15 Replies

5. Shell Programming and Scripting

awk for splitting file in constant chunks

Hi gurus, I wanted to split main file in 20 files with 2500 lines in each file. My main file conatins total 2500*20 lines. Following awk I made, but it is breaking with error. awk '{ for (i = 1; i <= 20; i++) { starts=2500*$i-1; ends=2500*$i; NR>=starts && NR<=ends {f=My$i".txt"; print >> f;... (10 Replies)
Discussion started by: mukesh.lalwani
10 Replies

6. Shell Programming and Scripting

Adding header to sub files after splitting the main file using AWK

Hi Folks, I have a file like: mainfile.txt: ------------- file1 abc def xyz file1 aaa pqr xyz file2 lmn ghi xyz file2 bbb tuv xyz I need output having two files file1 and file2. file1: ------ Name State Country abc def xyz aaa pqr xyz file2: (3 Replies)
Discussion started by: tanmay.gemini
3 Replies

7. Shell Programming and Scripting

awk - splitting 1 large file into multiple based on same key records

Hello gurus, I am new to "awk" and trying to break a large file having 4 million records into several output files each having half million but at the same time I want to keep the similar key records in the same output file, not to exist accross the files. e.g. my data is like: Row_Num,... (6 Replies)
Discussion started by: kam66
6 Replies

8. Shell Programming and Scripting

Splitting a complex file using awk

I have a file that contains the following format delete from table1; delete from table2; insert into table1 (col1, col2) values (value1, value2)@ insert into table1 (col1, col2) values(value3, value4)@ insert into table2(col1, col2,col3) values(value1, value2, value3)@ etc etc This is... (9 Replies)
Discussion started by: hukcjv
9 Replies

9. Shell Programming and Scripting

splitting tab-delimited file with awk

Hi all, I need help to split a tab-delimited list into separate files by the filename-field. The list is already sorted ascendingly by filename, an example list would look like this; filename001 word1 word2 filename001 word3 word4 filename002 word1 word2 filename002 word3 word4... (4 Replies)
Discussion started by: perkele
4 Replies

10. UNIX for Advanced & Expert Users

Help with splitting lines in a file using awk

I have a file which is one big long line of text about 10Kb long. Can someone provide a way using awk to introduce carriage returns every 40 chars in this file. Any other solutions would also be welcome. Thank you in advance. (5 Replies)
Discussion started by: martinbarretto
5 Replies
Login or Register to Ask a Question