Grouping or appending the lines in a file through Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grouping or appending the lines in a file through Unix
# 1  
Old 05-30-2012
Grouping or appending the lines in a file through Unix

Hi,

I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this

Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def 
A 1 100 abc def 
A 1 200 xyz def 
A 1 300 xyz def

I need it as shown below, Its Just to Group All records with 1 in the 2nd field

Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 1 100 abc def 
A 1 200 xyz def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

It would be really great if you can tell me a logic to regroup and remove certain unwanted or duplicate record in the 2nd occurence.. If you see above you can find that "A 1 100 abc def " is repeated twice... I would neeed an output like this

Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 1 200 xyz def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

Let me know if someone has idea on these lines

Last edited by joeyg; 05-30-2012 at 02:32 PM.. Reason: Please wrap data and scripts/commands with CodeTags.
# 2  
Old 05-30-2012
Seems a sort command would get you started.
Are you truly trying to sort on columns 2, then 4, and then 3?
# 3  
Old 05-30-2012
you could use for your sample data
Code:
sort -k1,1 -k2,2 -k4,4 infile

but ideally you should use
Code:
sort -k1,1 -k4,4 -k2,2 infile

# 4  
Old 05-30-2012
Actually joeyg, I just gave a sample.. My file is something similar in these lines but is very big with lot of lines and each line having lot of info... But when i used sort command, The output was getting jumbled...

Code:
A 1 100 abc def 
A 1 100 abc def 
A 1 200 abc def 
A 1 200 xyz def 
A 1 300 abc def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

But this is not what i am expecting

Last edited by Scrutinizer; 05-30-2012 at 05:32 PM.. Reason: code tags
# 5  
Old 05-30-2012
post what command you are using. Did you tried what is suggested in post #3
# 6  
Old 05-31-2012
expecting this ..
Code:
$ nawk '!x[$2$3]++' infile

# 7  
Old 05-31-2012
Use something like
Code:
sort -k1,1 -k2,2 -k4,4 tt | uniq

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending lines in a file

Hello, I have a file like: str1,"HEX"H,(39),info str2,"HEX"H,(854548),info str3,"HEX"H,'BGTOUR',info str4,"HEX"H,(534322),info str1,"HEX"H,,info str3,"HEX"H,'Landing',info str4,"HEX"H,'BG',info str1,"HEX"H,,info str3,"HEX"H,'Ay',info str1,"HEX"H,(27),info str2,"HEX"H,(854548),info... (2 Replies)
Discussion started by: apenkov
2 Replies

2. Shell Programming and Scripting

Need help on appending all the lines in a file after a pattern is found

Hi Friends, I am working on a file which has content as follows Wed,Database,ABC_cube,loaded Wed,Logging,out,user,302002654,active,for,0,minutes Wed,Logging,out,user,109000151,active,for,8,minutes Wed,Logging,out,user,302002654,active,for,0,minutes... (8 Replies)
Discussion started by: dev.devil.1983
8 Replies

3. Shell Programming and Scripting

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (1 Reply)
Discussion started by: mkandula1983
1 Replies

4. Shell Programming and Scripting

appending lines to a file dynamically

Hi all, I have a file with some number of lines. I need to add certain number of lines from another file which may vary according to the user's input and to it. eg code: I/P file 1 apps/file/xyz apps/file/abc apps/file/def file 2 progs/file/xyz ... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

5. Shell Programming and Scripting

Appending number of lines in the file to begining of the file

I am having 6 files named file1,file2....file6 and i need to append number of lines in each file to begining of the file. For example, If file 1 contains a b c d then after adding new line file1 should contain 4 a b c d Thanks in advance. (2 Replies)
Discussion started by: akhay_ms
2 Replies

6. Shell Programming and Scripting

Grouping each 2 lines

Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc... ex input: A B C D etc... ex output: 1A 1B 2C 2D etc... (5 Replies)
Discussion started by: watingo
5 Replies

7. UNIX for Advanced & Expert Users

grouping lines

Hi all, I have input lines like below: A;100;Paris;City;10;0;0 A;100;Paris;City;0;10;0 A;100;Paris;City in Europe;0;0;20 B;101;London;City;20;0;0 B;101;London;City;0;20;0 B;101;London;City in Europe;0;0;40 I need to group the above lines to: A;100;Paris;City in Europe;10;10;20... (4 Replies)
Discussion started by: andy2000
4 Replies

8. Shell Programming and Scripting

File splitting and grouping using unix script

Hello All, I have a small problem with file group/splitting and I am trying to get the best way to perform this in unix. I am trying with awk but need some suggestion what would be the best and fastest way to-do it. Here is the problem. I have a fixed length file with filled with product... (4 Replies)
Discussion started by: nandhan11891
4 Replies

9. Shell Programming and Scripting

grouping records in a file in unix

I have file like this d123, rahim, 140 d123, rahul, 440 d123, begum, 340 d234, bajaj, 755 d234, gajal, 657 I want to group this file like this d123, rahim, 140 rahul, 440 begum, 340 d234, bajaj, 755 gajal, 657 can any one help me on this thanks in advance (8 Replies)
Discussion started by: trichyselva
8 Replies

10. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies
Login or Register to Ask a Question