The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a column to a text based on file name rlapate Shell Programming and Scripting 12 05-23-2009 09:22 PM
Filtering records of a file based on a value of a column risk_sly UNIX for Dummies Questions & Answers 4 09-24-2008 02:22 AM
sorting file based on two or more columns labrazil Shell Programming and Scripting 2 09-16-2008 11:19 PM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 AM
Sorting a flat file based on multiple colums(using character position) cucubird Shell Programming and Scripting 8 07-25-2006 12:47 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-03-2009
tententen tententen is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 17
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 them based on the 3rd column first, then sort them based on 2nd column so it'll look like this

DE to DM;abc-ab-klm;critical
GT to KJ;def-ny-jrt;critical
US to UK;abc-hq-jcl;multimedia
HY to MC;la-yr-ytm;multimedia
UK to CN;def-ny-jkl;standard
FD to YM;la-yr-tym;standard

it would also be nice if somehow I can make it look like this so it'll look more presentable :P,

CRITICAL
DE to DM;abc-ab-klm;critical
GT to KJ;def-ny-jrt;critical

Multimedia
US to UK;abc-hq-jcl;multimedia
HY to MC;la-yr-ytm;multimedia

Standard
UK to CN;def-ny-jkl;standard
FD to YM;la-yr-tym;standard
  #2 (permalink)  
Old 07-03-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
hmmm use sort command
Code:
sort -t";" -k3 -k2 filename
and for second req use awk..
Code:
sort -t";" -k3 -k2 filename|awk -F\; '{A[$3]=A[$3]"\n"$0}END{for (i in A){print toupper(i)A[i]}}'
  #3 (permalink)  
Old 07-04-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
Using awk rule idea.
Code:
ff=file.txt

sort -t";" -k3 -k2 $ff | \
   awk -F";" -v keyfld=3 '
      # - next key value
      $keyfld != prevkey { 
                print toupper($keyfld) 
                }
      # - every line
                { 
                print $0
                prevkey=$keyfld
                }
   '
Or using ksh
Code:
prevkey=""
oifs="$IFS"
typeset -u key
sort -t";" -k3 -k2 $ff | while read line
do
        IFS=";"
        set -A flds -- $line    # to array, 1st index 0 
        IFS="$oifs"
        key=${flds[2]}
        [ "$prevkey" != "$key" ] && print "$key"
        print "$line"
        prevkey=$key
done
  #4 (permalink)  
Old 07-04-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 968
Because you said

Quote:
...it may have more columns
A slight modification to vidyadhar85's code
Code:
sort -t";" -k3 -k2 filename|awk -F\; '{A[$NF]=A[$NF]"\n"$0}END{for (i in A) {print toupper(i)A[i]}}'
  #5 (permalink)  
Old 07-05-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
how about below perl:

Code:
my @arr=<DATA>;
print map {$_->[0]}
      sort {$a->[1]->[2] cmp $b->[1]->[2] or $a->[1]->[1] cmp $b->[1]->[1]}
      map {my @tmp=split(";",$_);[$_,\@tmp]}
      @arr;
__DATA__
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
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0