split on the basis of 2nd and 3rd column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split on the basis of 2nd and 3rd column
# 1  
Old 07-29-2010
Java split on the basis of 2nd and 3rd column

file A
Code:
aa  22  48
ab 22  48
tcf 50 76
gf 50 76
h  89  100
yh 89   100

how can we split the file on the basis of common 2 and third column
output like
Code:
file A-1
aa  22  48
ab 22  48

file A-2
cf  50  76
gf  50   76

file A-3
th  89  100
yh 89   100

Moderator's Comments:
Mod Comment Having 120 posts you should be familiar with using code tags.

Last edited by zaxxon; 07-29-2010 at 04:49 AM..
# 2  
Old 07-29-2010
Code:
$> awk '$2==a && $3==b {print; next} {i++; if(NR==1){print "file A-"i}else{print "\nfile A-"i}; a=$2; b=$3; print}' infile
file A-1
aa  22  48
ab 22  48

file A-2
tcf 50 76
gf 50 76

file A-3
h  89  100
yh 89   100

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 07-29-2010
if separate output need be created then what can be the option

like file A-1,fileA-2 etc...
# 4  
Old 07-29-2010
I don't like spaces in file names so I added an underscore:
Code:
awk '$2==a && $3==b {print >> "file_A-"i; next} {i++; a=$2; b=$3; print >> "file_A-"i}' infile
$> ll
total 24
drwxr-xr-x 2 root root 4096 29. Jul 14:06 .
drwxr-x--- 3 isau isau 4096 29. Jul 14:05 ..
-rw-r--r-- 1 root root   21 29. Jul 14:06 file_A-1
-rw-r--r-- 1 root root   23 29. Jul 14:06 file_A-2
-rw-r--r-- 1 root root   24 29. Jul 14:06 file_A-3
-rw-r--r-- 1 root root   68 29. Jul 14:05 infile
$> cat file_A-1
aa  22  48
ab 22  48
$> cat file_A-2
cf  50  76
gf  50   76
$> cat file_A-3
th  89  100
yh 89   100

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split large file into 24 small files on one hour basis

I Have a large file with 24hrs log in the below format.i need to split the large file in to 24 small files on one hour based.i.e ex:from 09:55 to 10:55,10:55-11:55 can any one help me on this.! ... (20 Replies)
Discussion started by: Raghuram717
20 Replies

2. UNIX for Beginners Questions & Answers

Split a txt file on the basis of line number

I have to split a file containing 100 lines to 5 files say from lines ,1-20 ,21-30 ,31-40 ,51-60 ,61-100 Here is i can do it for 2 file but how to handle it for more than 2 files awk 'NR < 21{ print >> "a"; next } {print >> "b" }' $input_file Please advidse. Thanks (4 Replies)
Discussion started by: abhaydas
4 Replies

3. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

4. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

5. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies

6. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

7. Shell Programming and Scripting

print least value of a column on the basis of another column

Hi, I am new to linux... I have a file which looks like: I want to print the entire row in which 5th column is having minimum value for every first column (i.e min for 9 and min for 16). Along with the condition awk -F" " 'b < $5 {b=$5; a=$0} END {for (i in a) {print a}}' inputfile >... (5 Replies)
Discussion started by: CAch
5 Replies

8. Shell Programming and Scripting

split the file based on the 2nd column passing as a parameter

I am unable to spit the file based on the 2nd column passing as a parameter with awk command. Source file: “100”,”customer information”,”10000” “200”,”customer information”,”50000” “300”,”product information”,”40000” script: the command is not allowing to pass the parameters with the awk... (7 Replies)
Discussion started by: number10
7 Replies

9. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

10. Shell Programming and Scripting

Split/create directories on basis of their size

Hi, I would really apprciate it if someone expert in shell, "shell guru" scripting can help me. with a shell command how can I create directories and populate on basis of size. Before I confuse you, let me tell you the scenerio. We take hot database backup of oracle on disk, and... (0 Replies)
Discussion started by: amitoverseas40
0 Replies
Login or Register to Ask a Question