Split file into given category and others using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split file into given category and others using awk
# 1  
Old 07-30-2011
Split file into given category and others using awk

Hi All,

Would it be possible using awk to split a given file into two files based on a certain condition such that one output file will contain all lines that fit the condition while the other output file will contain lines that did not fit the condition?

Here is a sample input file

input.txt
Code:
1|2|3|ABC|ZXC|456
7|9|1|DEF|YTR|521
0|3|8|ABC|TFT|896
0|1|0|RVB|PLT|683

And this should be the contents of the output files
input.txt.ABC
Code:
1|2|3|ABC|ZXC|456
0|3|8|ABC|TFT|896

input.txt.OTHERS
Code:
7|9|1|DEF|YTR|521
0|1|0|RVB|PLT|683

Thanks for the help in advance!
# 2  
Old 07-30-2011
Hi,
try this and let me know if it works.
Code:
 awk '{FS="|"} {print $6}' filename > output file


Regards,
Mayur

---------- Post updated at 11:39 AM ---------- Previous update was at 11:36 AM ----------

Hi,
Sorry for the didn't go through the query properly just some loss of concentration.


Regards,
Mayur
# 3  
Old 07-30-2011
Hi Mayur,

I tried the the script that you sent but it only outputs the sixth field of the input file.

output.txt
Code:
456
521
896
683

# 4  
Old 07-30-2011
Code:
awk -F'|' '$4 == "ABC" {print > "ABC.file" }
           $4 != "ABC" {print > "OTHERS.file"}' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 5  
Old 07-30-2011
Hi,
try this
Code:
 awk '{for (i=1,i<NR,i++) if(NR%2==0) print $0 >> file1 else print $0 >> file2 }' filename


Regards,
Mayur
# 6  
Old 07-30-2011
Hi Yazu,

Thanks for the script it was what I needed.

Quote:
Originally Posted by yazu
Code:
awk -F'|' '$4 == "ABC" {print > "ABC.file" }
           $4 != "ABC" {print > "OTHERS.file"}' INPUTFILE

---------- Post updated at 02:49 PM ---------- Previous update was at 02:46 PM ----------

Hi Mayur,

I am getting the following error when running your script

Code:
$ awk '{for (i=1,i<NR,i++) if(NR%2==0) print $0 >> "file1" else print $0 >> "file2" }' input.txt
 syntax error The source line is 1.
 The error context is
                {for >>>  (i=1, <<< 
 awk: The statement cannot be correctly parsed.
 The source line is 1.
 awk: The statement cannot be correctly parsed.
 The source line is 1.


Quote:
Originally Posted by mayursingru
Hi,
try this
Code:
 awk '{for (i=1,i<NR,i++) if(NR%2==0) print $0 >> file1 else print $0 >> file2 }' filename


Regards,
Mayur
# 7  
Old 08-02-2011
Hi cympaulife,
try this will get the required output.
Code:
 
#!/bin/bash
awk '{
{
if(FNR%2==0)
print $0 > "file1"
else
print $0 > "file2"
}
} pg' filename

Also yazu's solution is also great.

Regards,
Mayur
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Category and count with awk

I want to categorize and count the as below: Input file: A1 G1 C1 F1 A2 G1 C1 F1 A3 G1 C1 F2 A4 G1 C2 F2 A7 G1 C2 F2 A8 G1 C2 F3 A11 G1 C2 F3 A23 G1 C2 F3 B4 G1 C2 F3 AC4 G2 C3 F4 B6 G2 C4 F4 BB5 G2 C4 F4 A25 G2 C5 F4 B13 G2 C5 F5 D12 G2 C5 F5 D2 G2 C5 F5 (3 Replies)
Discussion started by: aydj
3 Replies

2. Shell Programming and Scripting

Total count in each category for given file list

I have list of file names in filename.txt below is file format >>File1 _________________________ 01~12345~Y~YES~aaaaa~can 02~23456~N~NO~bbbbb~can . . . 99~23__________________________ Need to find total count from each file depending on specific string and add them to have total count... (17 Replies)
Discussion started by: santoshdrkr
17 Replies

3. Shell Programming and Scripting

awk file split

Hi all, First of all I' like to mention that I'm pretty new to unix scripting. :( I'm trying to split an large xml with awk and rename it based on the values of two attributes. Example XML <RECORD> <element1>11</element1> <element2>22</element2> <element3>33</element3>... (18 Replies)
Discussion started by: f0usk4s
18 Replies

4. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

5. Shell Programming and Scripting

AWK File Split

Hi All, Input.txt XYZONEABC                  CZXTWOJJJ KKKSIXOOO asdfhajlsdhfajs asdfasfasdf Output Files: ONE.txt XYZONEABC                 TWO.txt CZXTWOJJJ SIX.txt KKKSIXOOO I had a script (2 Replies)
Discussion started by: kmsekhar
2 Replies

6. Shell Programming and Scripting

Getting category when given the variable from external file to shell script

Hi, I have a script that interacts with a config file in the format: file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt I would like to return the Category, when given the file name. (11 Replies)
Discussion started by: MoreCowbell
11 Replies

7. Shell Programming and Scripting

split a file using awk

Hi , I just need to split a file and outputfiles are redirected to gzip file need: Input file - A.gz content of A.gz is 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200212|sdfdf 100|sfdds|dffdds|200212|sdfdf... (3 Replies)
Discussion started by: mohan_xunil
3 Replies

8. Shell Programming and Scripting

split file with awk

I did a lot of search on this forum on spiting file; found a lot, but my requirement is a bit different, please guide. Master file: x:start:5 line1:23 line2:12 2:90 x:end:5 x:start:2 45:56 22:90 x:end:2 x:start:3 line1:23 line2:12 x:end:3 x:start:2 line5:23 (1 Reply)
Discussion started by: uwork72
1 Replies

9. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

10. Shell Programming and Scripting

Split file using awk

I am trying to read a file and split the file into multiple files. I need to create new files with different set of lines from the original file. ie, the first output file may contain 10 lines and the second 100 lines and so on. The criteria is to get the lines between two lines starting with some... (8 Replies)
Discussion started by: pvar
8 Replies
Login or Register to Ask a Question