Need help in writing a routine for sorting a CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in writing a routine for sorting a CSV file
# 1  
Old 04-11-2011
Need help in writing a routine for sorting a CSV file

Hi,

I have a CSV file in following manner:

server1,env1,patch1
server1,env1,patch2
server1,env1,patch3
server1,env2,patch1
server1,env2,patch3
server2,env3,patch1
server2,env3,patch5
server2,env4,patch1
server3,env6,patch1
server3,env7,patch2
server3,env7,patch3

I want to sort this input on server basis and then run execute the script. Something liek below:

sort these values and run the script
server1,env1,patch1
server1,env1,patch2
server1,env1,patch3
server1,env2,patch1

Once above operation is done

sort these values and run the script
server2,env3,patch1
server2,env3,patch5
server2,env4,patch1

followed by

sort these values and run the script:
server3,env6,patch1
server3,env7,patch2
server3,env7,patch3



The script that I am trying to execute has inbuilt logic wrriten to take this sorted values as input and perform operation.

Any help here would be really appreciated.

Thanks in advance,
Avikal Jain
# 2  
Old 04-11-2011
Not sure if this is what you were asking for:

Code:
cut -f1 -d"," your_file.csv | sort -u | while read server
do
     grep "^${server}," your_file.csv | your_inbuilt_script.sh
done

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 04-11-2011
Thanks Chubler_XL, this is exactly what I was looking for.
# 4  
Old 04-13-2011
Hi,

I am facing an issue now with my script where I am using this routine, while running it gives me following error:
Code:
/mpp/t24_delivery/RCM/menu/packlist.dat: line too long.

Any idea what might have been causing this? This runs fine on my test machine however on actual environment the script gives the above error.

The complete command that I am running is:
Code:
cut -f1 -d"," "${CONFIG_FILE_PATH}" | sort -u | while read HOST_NAME
do
 grep "^${HOST_NAME}," "${CONFIG_FILE_PATH}" > /mpp/t24_delivery/RCM/menu/deployment/$HOST_NAME.dat

where ${CONFIG_FILE_PATH} = /mpp/t24_delivery/RCM/menu/packlist.dat

Thanks for help in advance.

-Avikal Jain

Last edited by Franklin52; 04-13-2011 at 08:32 AM.. Reason: Please use code tags
# 5  
Old 04-14-2011
Sounds like grep is failing because your datafile packlist.dat has a line with too may characters. From memory, grep does not support more that about 2K characters in 1 line.
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 04-14-2011
Quote:
Originally Posted by Chubler_XL
Not sure if this is what you were asking for:

Code:
cut -f1 -d"," your_file.csv | sort -u | while read server
do
     grep "^${server}," your_file.csv | your_inbuilt_script.sh
done

Code:
awk -F, '!a[$1]++{print $1}' your_file.csv |while read server

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 04-15-2011
Hi Chubler_XL, I do not think the grep command is the issue here, I replaced the grep command with just echo $HOST_NAME and I still get the same error message:

The code:

Code:
 
cut -f1 -d"," "${CONFIG_FILE_PATH}" | sort -u | while read HOST_NAME
do
echo $HOST_NAME

The error message i get using above is:
stdin: line too long

I checked my input file, when I perform a cat on it the ouput is something like below:

ROOT@XXXXXXXXXX:/tmp # cat packlist.dat
server1,SG11,PACK1
server1,SG11,PACK2
server1,SG11,PACK3
server1,SG12,PACK1
server1,SG12,PACK2
server1,SG12,PACK3
server2,SG21,PACK1
server2,SG21,PACK2
server2,SG21,PACK3
server2,SG22,PACK1
server2,SG22,PACK2
server3,SG31,PACK1ROOT@XXXXXXXXXXX:/tmp #

However when I add extra line at the end of the file, output of cat seems like below:

ROOT@XXXXXXXXXX:/tmp # cat packlist.dat
server1,SG11,PACK1
server1,SG11,PACK2
server1,SG11,PACK3
server1,SG12,PACK1
server1,SG12,PACK2
server1,SG12,PACK3
server2,SG21,PACK1
server2,SG21,PACK2
server2,SG21,PACK3
server2,SG22,PACK1
server2,SG22,PACK2
server3,SG31,PACK1
ROOT@XXXXXXXXXXX:/tmp #

and the same code works fine, I guess I need the routine which can sort the csv doesnt matter if teh file has extra line at the end or not.

@rdcwayx : I will give a try to your routine as well.

Thanks for your input everyone, really appreciate your help. I will post the results once try rdcwayx's routine.


---------- Post updated at 11:25 PM ---------- Previous update was at 10:37 PM ----------

Folks, the awk utility provided by rdcwayx is working fine, the only question I have now is that will it able to handle any kind of input file i.e the one with a extra line and one without a extra line at the end?

Many thanks again to rdcwayx and Chubler_XL for their time and input.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Writing multiple outputs in to separate cells of CSV file

Hi I am writing a script which has multiple awk statements and each statement gives me a numeric count as an output. I want those output to be stored in different cells of a csv file. say 12 awk statements give 12 output and i want them in diffrenet cells of csv file. Thank you guys..!! (4 Replies)
Discussion started by: prabhat.diwaker
4 Replies

2. 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

3. Shell Programming and Scripting

Sorting the csv file in Perl

Hi All How all are doing today. Just struck in an issue in Perl I have a csv file which contain 32 column, I want to make sorting in that csv file with respect to 26th column. Is it possible to do so without any module being added? Regards Aditya ---------- Post updated at 10:02 AM... (11 Replies)
Discussion started by: adisky123
11 Replies

4. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: strangemachine
5 Replies

5. Shell Programming and Scripting

Reading from a CSV and writing in same CSV file

Hi, I am tryng to read from a csv file and based on some grep command output I will modify one of the column in the same csv. Example:- Input CSV:- 20120829001415,noneAA,google.com 20120829001415,dfsafds,google.com 20120829001415,noneAA,google.com Intermediate Step:- If 2nd column of... (3 Replies)
Discussion started by: kmajumder
3 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

writing to a csv file

Hi buddies i have doubt. I 'm new to unix script and learning. I have a req. I need to find the values mentioned in the double quoutes of each line and write it into a csv file. How do I handle this? Please help me. SCTY_ISS_ID (SCTY_ISS_ID:Dec18Fixed.0:): "37927320080309" TRD_DT... (6 Replies)
Discussion started by: pstanand
6 Replies

10. UNIX for Dummies Questions & Answers

Writing a shared library --- any initialization routine?

Hi, all. IM new to this forum. I wanted to write a shared library for my program. However, I need the library to have some self initialization routines before anyother functions in the library are called. Like the ones available for gcc __attribute__ ((constructor)). I tried int... (1 Reply)
Discussion started by: rogeryin
1 Replies
Login or Register to Ask a Question