Sinisterly
Bash Help Wanted - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: Bash Help Wanted (/Thread-Bash-Help-Wanted)



Bash Help Wanted - unixbreak - 03-13-2014

Hello guys, I'm stuck in a problem for few days. Here it is maybe u got bigger brains than me!

I got a bunch of CSV files and i want them concatenated into a single .csv file, numeric sorted. Ok, first encountered problem is with the ID (i want to sort unly by ID) name.
eg

sort -f *.csv > output.csv This would work if i had standard ids like id001, id002, id010, id100
but my ids are like id1, id2, id10, id100 and this make my sort job inaccurate.

Ok

sort -t, -V *.csv > output.csv - This works perfectly on my test machine (sort --version GNU coreutils 8.5.0) but my live machine from work got 5.3.0 sort version (and they didn't had implemented -V syntax on it) and i cannot update it!

I'm feel so noob and unlucky
If you feel like you have a better idea please bring it on.

my csv file looks like

,id1,aaaaaa,bbbbbbbbbb,cccccccccccc,ddddddd
,id2,aaaaaa,bbbbbbbbbb,cccccccccccc,ddddddd
,id10,aaaaaa,bbbbbbbbbb,cccccccccccc,ddddddd
,id40,aaaaaa,bbbbbbbbbb,cccccccccccc,ddddddd
,id101,aaaaaa,bbbbbbbbbb,cccccccccccc,ddddddd
,id201,aaaaaaaaa,bbbbbbbbbb,ccccccccccc,ddddddd

Looking forward reading you!
Regards


RE: Bash Help Wanted - Deque - 03-14-2014

Do something like:

Quote:sed 's/^.\{1\}//' tosort | perl -we '$, = "\n"; print sort { substr($a,2, index($a, ",")-2) <=> substr($b,2, index($b, ",")-2) } <>'

sed command is used to remove the trailing comma.


RE: Bash Help Wanted - unixbreak - 03-27-2014

Thank you Deque

I've create a perl script that works good

Code:
#!/usr/bin/perl $infile = $ARGV[0]; if (!-e $infile){ $infile = "unsortedfile.csv"; }; open (IN, "<$infile"); @raw = <IN>; close (IN); foreach $curline(@raw){ $curline =~ s/\n//g; @pieces = split(",", $curline); $id = substr($pieces[1], 2); $tmphash{$id} = $curline; }; foreach $key (sort {$a <=> $b} keys %tmphash){ print $tmphash{$key} . "\n"; };

This is it in case someone of you need it for similar tasks
run it as
Code:
perl script.pl unsortedfile.csv > sorted.csv
cheers