![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
| sort certain patten ???not the whole file? | nabmufti | Shell Programming and Scripting | 6 | 02-14-2008 10:54 AM |
| add columns from file to another and sort | kamel.seg | Shell Programming and Scripting | 12 | 12-12-2007 02:39 PM |
| How to Sort a file for given situation? | vishalpatel03 | Shell Programming and Scripting | 4 | 11-26-2007 10:53 AM |
| Sort and compare file | sabercats | Shell Programming and Scripting | 3 | 03-27-2006 03:05 PM |
| how to sort a file | tao | UNIX for Dummies Questions & Answers | 2 | 03-24-2002 11:34 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
sort csv file
hi guys.
i am quite new to shell scripting. and i've been practicing coding in a CSV (comma separated value) file is it possible to sort a specific block? what do i do if i want to sort these contacts by their birthdays in ascending order (Jan. to Dec.)? And i want only to display their name and birthday? Here is the file Sharapova,Maria,19-apr-1987,Brandenton FLorida,USA,admin@mariasharapova.com,+1(954)3387488 Bryant,Kobe,23-aug-1978,L.A. California,USA,kobe@lakers.comk+1(310)87890 Spielberg,Steven,18-dec-1946,L.A.California,USA,steven@dreamworks.com,+1(389)89098 Harnois,Elisabeth,26-may-1979,Detroit Michigan,USA,liz@1treehill.net,+1(313)74564536 Ryan,Michelle,22-apr-1984,Enfield London,UK,mryan@nbc.com,+44(207)2988410 Belle,Camilla,2-oct-19886,Miami Florida,USA,cam_65@hollywood.org, +1(954)3790491 Gellar,Sarah Michelle,14-apr-1977,L.A. California,USA,smg@buffy.com,+1(213)3398848m Underwood,Carrie,10-mar-1983,Checotah Oklahoma,USA,carrieunderwood@rca.com,+1(918)655423 many thanks!!! |
|
||||
|
Well, hopefully I did this for more than my own amusement:
Code:
#!/usr/bin/perl
use warnings;
use strict;
my @months_numeric = qw(01 02 03 04 05 06 07 08 09 10 11 12);
my @months_alphas = qw(jan feb mar apr may jun jul aug sep oct nov dec);
my %months;
@months{@months_alphas} = @months_numeric;
open (my $in , '<', 'path/to/celebs.csv') or die "$!";
my @sorted = map {$_->[0]}
sort{$a->[1] cmp $b->[1]}
map {chomp;
my $bd = (split(","))[2];
my ($d,$m,$y) = split("-",$bd);
$d = $d<10 ? "0$d" : $d;
[$_,"$y$months{$m}$d"]} <$in>;
close $in;
print "$_\n" for @sorted;
|
|
||||
|
Quote:
Code:
sort -t"," -k 3 -t"-" -k 2 -M birthdays.txt |awk -F"," '{print $1, $2, $3}'
|
|
||||
|
sed+sort
Hi,
This one should be ok for you. Code:
sed ' s/\([0-9]*\)-jan-\([0-9]*\)/\201\1/; s/\([0-9]*\)-feb-\([0-9]*\)/\202\1/; s/\([0-9]*\)-mar-\([0-9]*\)/\203\1/; s/\([0-9]*\)-apr-\([0-9]*\)/\204\1/; s/\([0-9]*\)-may-\([0-9]*\)/\205\1/; s/\([0-9]*\)-jun-\([0-9]*\)/\206\1/; s/\([0-9]*\)-jul-\([0-9]*\)/\207\1/; s/\([0-9]*\)-aug-\([0-9]*\)/\208\1/; s/\([0-9]*\)-sep-\([0-9]*\)/\209\1/; s/\([0-9]*\)-oct-\([0-9]*\)/\210\1/; s/\([0-9]*\)-nov-\([0-9]*\)/\211\1/; s/\([0-9]*\)-dec-\([0-9]*\)/\212\1/ ' filename > filename.temp sort -t"," +2 filename.temp | cut -d"," -f1,2,3 rm filename.temp |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| linux, linux commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|