[SOLVED] Sorting into new files based on column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Sorting into new files based on column
# 1  
Old 05-15-2012
[SOLVED] Sorting into new files based on column

Hi,
I have the following file, I need to sort it based on a column and write to different output files based on this column

Code:
request_guid    iso_country_cd  address_data    response_time
32895901-d17f-414c-ac93-3e7e0f5ec240    AND     BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND; PostalCode:AD200; LanguageCode:GER;      30003
73b129e1-1fa9-4c0d-b95b-4682e5389612    AUS     BaseName:MARKET STREET; HouseNumber:68; Builtup:SYDNEY; ISOCountryCode:AUS; PostalCode:NSW 2000; LanguageCode:GER;      20443
40f82e88-d1ff-4ce2-9b8e-d827ddb39447    BEL     BaseName:CHAUSSEE DE CHARLEROI; HouseNumber:38; Order8:BRUSSELS; ISOCountryCode:BEL; PostalCode:1060; LanguageCode:GER;         10121
36e9c3f1-042a-43a4-a80e-4a3bc2513d01    BGR     BaseName:BULGARIA BOULEVARD; HouseNumber:1; Builtup:SOFIA; ISOCountryCode:BGR; PostalCode:1421; LanguageCode:GER;       17295
243c2a71-3a32-49c7-9759-0e479bcac1ff    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  10852
db4a1785-df14-4165-864c-ecee4bf7770d    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  13657
5bfcbfa1-37d1-48ab-9275-bf20f0d08e61    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  17145
edaf5a71-731c-4c41-a54d-586709858337    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  30005
9dffdb0c-4fe7-4bda-a86f-bbb585043e7a    CAN     BaseName:12TH AVE SW; HouseNumber:10020; Order8:EDMONTON; ISOCountryCode:CAN; PostalCode:T6X 0P9; LanguageCode:ENG;     14796
ded016e1-015b-46dc-9f80-0a3d58ae7d72    CAN     BaseName:97 ST NW; HouseNumber:13620; Order8:EDMONTON; ISOCountryCode:CAN; PostalCode:T5E 4E2; LanguageCode:ENG;        16114
2f0a5d11-7b71-49ae-b958-f9601e89424d    CHE     BaseName:BADENSTRASSE; HouseNumber:1; Order8:BASEL; ISOCountryCode:CHE; PostalCode:CH-4019; LanguageCode:GER;   21866
e283e62c-85b8-4229-993b-50f40aa627b7    CZE     BaseName:CTIRADOVA; HouseNumber:16; Order8:PRAGUE; ISOCountryCode:CZE; PostalCode:16000; LanguageCode:GER;      30004
cade5864-6162-42d9-bacf-99f1e92cc5da    CZE     BaseName:LIDICKA; HouseNumber:23; Order8:BRNO; ISOCountryCode:CZE; PostalCode:659 89; LanguageCode:GER;         30004
aed652f3-ce95-4160-9a57-3ac0d0623fe0    DEU     BaseName:UFERHALLEN UFERSTR; HouseNumber:8; Order8:BERLIN; ISOCountryCode:DEU; PostalCode:13357; LanguageCode:GER;      14195
970cedca-4b2b-4752-84ed-f120a1e6d90a    DEU     BaseName:IM FORUM; HouseNumber:117; Order8:BERLIN; ISOCountryCode:DEU; PostalCode:10407; LanguageCode:ENG;      20444
1518d5ae-efe9-4957-96e3-bcfe4fbddddd    DEU     BaseName:PIEFKE PETRICK UND NOWOTNIK GBR ZINZENDORFSTR; HouseNumber:18; Order8:BERLIN; ISOCountryCode:DEU; PostalCode:10555; LanguageCode:GER;  30003
9504b870-2d3e-4a70-b724-ff2252560847    DEU     BaseName:NUERNBERGER STR; HouseNumber:33-34; Order8:BERLIN; ISOCountryCode:DEU; PostalCode:10777; LanguageCode:GER;     30004
ca8a30be-cd13-4876-a374-803434fd9764    DNK     BaseName:, SMEDEGADE; HouseNumber:2; Builtup:HERNING; ISOCountryCode:DNK; PostalCode:7490; LanguageCode:DAN;    30003
34fdcb49-f831-4a46-9249-b56d52d21a37    DNK     BaseName:, FASANVEJ; HouseNumber:3; Builtup:FREDERIKSHAVN; ISOCountryCode:DNK; PostalCode:9900; LanguageCode:DAN;       30004
b0d7c2c0-c500-4da5-828f-e595975ba180    ESP     BaseName:CALLE DE LA SOLEDAD; HouseNumber:12; Order8:MADRID; ISOCountryCode:ESP; PostalCode:28194; LanguageCode:SPA;    10644

I have used sort -k2 to sort the file;
I want my output files according to the country code

AND.txt
Code:
request_guid    iso_country_cd  address_data    response_time
32895901-d17f-414c-ac93-3e7e0f5ec240    AND     BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND; PostalCode:AD200; LanguageCode:GER;      30003

AUS.txt
Code:
request_guid    iso_country_cd  address_data    response_time
73b129e1-1fa9-4c0d-b95b-4682e5389612    AUS     BaseName:MARKET STREET; HouseNumber:68; Builtup:SYDNEY; ISOCountryCode:AUS; PostalCode:NSW 2000; LanguageCode:GER;      20443

BRA.txt
Code:
request_guid    iso_country_cd  address_data    response_time
243c2a71-3a32-49c7-9759-0e479bcac1ff    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  10852
db4a1785-df14-4165-864c-ecee4bf7770d    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  13657
5bfcbfa1-37d1-48ab-9275-bf20f0d08e61    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  17145
edaf5a71-731c-4c41-a54d-586709858337    BRA     BaseName:MARECHAL MALLET; HouseNumber:545; Order8:PRAIA GRANDE; Order1:SÃO PAQULO; ISOCountryCode:BRA; PostalCode:11700-400; LanguageCode:ENG;  30005

how do i get this

---------- Post updated at 10:01 AM ---------- Previous update was at 09:50 AM ----------

awk '$2 ~ /AND/{print $0 >> "AND.txt"}; $2 ~ /AUS/{print $0 >> "AUS.txt"}' infile

---------- Post updated at 10:03 AM ---------- Previous update was at 10:01 AM ----------

Sorry for posting it before searching.. here is how i did it

awk '$2 ~ /AND/{print $0 >> "AND.txt"}; $2 ~ /AUS/{print $0 >> "AUS.txt"}' infile
This User Gave Thanks to ramky79 For This Post:
# 2  
Old 05-15-2012
Glad you got it working, and thank you for returning to the topic to say so. I've marked it 'solved'.

'print $0' is redundant by the way. Just 'print >> filename' will do.

You don't need a regex there either, since that might match more than you want -- it could match AAUS, not just AUS, for instance. ($2 == "AUS") will match the exact string only.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

2. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies

5. UNIX for Dummies Questions & Answers

Sorting a file based on the absolute value of a column

I would like to sort a tab delimited text file based on the absolute value of its second column. How do I go about doing that? Thanks! Example input: A -12 B 0 C -6 D 7 Output: A -12 D 7 C -6 B 0 (4 Replies)
Discussion started by: evelibertine
4 Replies

6. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

7. Emergency UNIX and Linux Support

[Solved] Extract records based on a repeated column value

Hi guys, I need help in making a command to find some data. I have multiple files in which multiple records are present.. Each record is separated with a carriage return and in each record there are multiple fields with each field separated by "|" what i want is that I want to extract... (1 Reply)
Discussion started by: m_usmanayub
1 Replies

8. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

9. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies

10. UNIX for Dummies Questions & Answers

Sorting list of files per date column

Hi all, I have a pecular issue in sorting these files (not an ls -lrt) in Solaris environment. All the below files are modified on November 4th, but I want to sort these files as per date (eg: 01May07_1623 = ddmmmyy_hhmm) Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg Nov 4 18:27... (10 Replies)
Discussion started by: shivaastrogun
10 Replies
Login or Register to Ask a Question