CSV Sorting on only particular columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSV Sorting on only particular columns
# 1  
Old 10-30-2012
CSV Sorting on only particular columns

Hello!
So ive been presented with this comma-delimited file:
I need a print to look as below
"
lastname, phone_number, zip

for every person with a last name starting with the letter H, I

only with a 650-area code phone number. output should be sorted by reverse ZIP code "

I only have four columns

Harris, K, (650)345-1760 1200, Ellis Street Suite 230 Mountain CA 93042
Mitchell, A, (212)-555-1234 650, This Street NY NY 01234
Thomson, F, (650)-999-9999 1450, The Terrace Avenue Stratford MA 10101
irvin, h, (650)678-2445 12345, denver st santa clara 96503


I thought I was on the right path with this code:
Code:
cat data.csv | awk -F"," '{print $1, $2, $3, $4}' |sort -t ',' -k1|grep "(650)"

then to try to grep just the last name's first letter only...

Code:
cat data.csv | awk -F "," '$1~/"H*"/' '{print $1, $2, $3, $4}'

but keep getting errors.
How can I search for just the first letter as well as only part of a string in the last column and reverse order that?(I imagine with sort?)

Last edited by strangemachine; 10-30-2012 at 03:04 PM..
# 2  
Old 10-30-2012
Assuming you want records starting with h and i and contains zip code of 650

try

Code:
grep -i -E "^h|^i" data.csv | grep "(650)"

# 3  
Old 10-30-2012
It returns zero results, if i use just that syntax or append it to mine.
# 4  
Old 10-31-2012
Quote:
Originally Posted by strangemachine
It returns zero results, if i use just that syntax or append it to mine.
What you are trying..?


Code:
$ cat file
Harris, K, (650)345-1760 1200, Ellis Street Suite 230 Mountain CA 93042
Mitchell, A, (212)-555-1234 650, This Street NY NY 01234
Thomson, F, (650)-999-9999 1450, The Terrace Avenue Stratford MA 10101
irvin, h, (650)678-2445 12345, denver st santa clara 96503

$ grep -i -E "^h|^i" file | grep "(650)"
Harris, K, (650)345-1760 1200, Ellis Street Suite 230 Mountain CA 93042
irvin, h, (650)678-2445 12345, denver st santa clara 96503

# 5  
Old 10-31-2012
I believe that the following command does what was requested:
Code:
awk '/^[HhIi]/{printf("%s,%s\n", $NF, $0)}' input|sort -rn|awk 'BEGIN{FS=OFS=","}$4~/(650)/{print $2, $4, $1}'

The first awk selects last names starting with H or I (case insensitive) and adds a new 1st field just containing the ZIP code. The sort performs a reverse order sort on the ZIP code. The second awk selects phone numbers in area code 650 and prints just the last name, phone, and zip code in the desired order. For the given input, the result is:
Code:
irvin, (650)678-2445 12345,96503
Harris, (650)345-1760 1200,93042

# 6  
Old 10-31-2012
with single awk... and sort

Code:
$awk -F, '/^[HhIi]/ && $3 ~ /(650)/{n=split($NF,P," ");print $1,$3,P[n]}' OFS="," file | sort -t, -nrk3

irvin, (650)678-2445 12345,96503
Harris, (650)345-1760 1200,93042

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace columns in .csv using other .csv columns

Hi, I have 2 csv files with 15 000 lines, which looks like this: Daily.csv "CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION","IMAGE","COST","WEIGHT","MSRP","UPC" "M-1001","212","Caroline Her","1.7 oz","EDT... (4 Replies)
Discussion started by: olivieraz
4 Replies

2. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Sorting a CSV file by DOB

I have absolutaly no idea how to get this script to sort the info in Birthdays.csv by date of birth. I know the sort -n command, however i wish to sort the file birthdays.csv by DOB. How would i go about doing this? The below script gets user info and date of birth and then puts these info a... (3 Replies)
Discussion started by: redshine6
3 Replies

6. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

7. Shell Programming and Scripting

Sorting a .csv using awk or other

Hello all, I am new here and *relatively* new to Unix. I have a bit of an emergency. I have a three column file that I need to sort: sample name, miRNA, reads per million (RPM) There are multiple samples, and for each sample name there are multiple miRNAs and associated RPMs. Some of these... (6 Replies)
Discussion started by: dunnybocter
6 Replies

8. UNIX for Advanced & Expert Users

Unix Bash: substitute columns in .csv using other .csv columns

Hi All, I have two .csv's input.csv having values as (7 columns) ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006 XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010 output.csv having (25 columns) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y... (4 Replies)
Discussion started by: abhivyas
4 Replies

9. UNIX and Linux Applications

UNIX sorting - csv file

Hi, Please help me to solve sorting in CSV file. I have 25 columns in my CSV. (Delimiter is ",") Summary columns are from 10 to 13 which are should not be sorted. From 1-9 and 14-25 should be able to sort in ascending. how should i do this using Sort command in unix. If no simple commands pls... (0 Replies)
Discussion started by: rajani_p
0 Replies

10. Shell Programming and Scripting

Help sorting .csv file

Hi, I have a .csv file which contains script names, subjects and email_addresses The first two colums are always script name and subject, the next 20 colums are email address.. What i want to do is sort the email address in alphabetical order for each row and there's around 1200 rows. So... (3 Replies)
Discussion started by: Jazmania
3 Replies
Login or Register to Ask a Question