sort each column of text file alone


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort each column of text file alone
# 8  
Old 05-11-2011
i don't want to remove anything , i just want the same script to order the data , but i changed the special characters that are before the string (, become -)and(;become +)
so what i should change in the script to adopt these changes ??

---------- Post updated at 04:35 AM ---------- Previous update was at 03:41 AM ----------

and btw, the awk that yinyuemi did not worked at all , it has some erros , but the perl script that bartus suggested works but do not give the needed results , it sort some of the data but not all of them.
# 9  
Old 05-11-2011
Try:
Code:
perl -nae '@F[2..$#F]=sort {$x=$a;$y=$b;$x=~s/[-+]//;$y=~s/[-+]//;$x cmp $y} @F[2..$#F];print "@F\n"' file

This User Gave Thanks to bartus11 For This Post:
# 10  
Old 05-11-2011
Thank you a lot bartus11 , it worked

but i found out that it will add a + sign to the 2nd column , why ?

Last edited by vbe; 05-11-2011 at 07:59 AM.. Reason: merge...
# 11  
Old 05-11-2011
Can you show some sample data for which this code is producing unwanted output?
# 12  
Old 05-11-2011
+a1 -AB +CE
+a2 -QR -RV
+a3 -UX -WC

---------- Post updated at 06:31 AM ---------- Previous update was at 06:22 AM ----------

ok , i found a way to remove the + sign using cut , btw thanks a lot for your help
# 13  
Old 05-13-2011
Hi.

I often find it cumbersome to craft a specialized sort routine with the builtin perl sort. There is a module that, while not trivial, will make a sorting function for you. Here's an example of that, driven by a shell script:
Code:
#!/usr/bin/env bash

# @(#) s4	Demonstrate short, flexible perl code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl

FILE=${1-data1}

pl " perl code:"
cat p1

pl " Input file $FILE:"
cat $FILE

pl "Results:"
./p1 $FILE

exit 0

producing:
Code:
% ./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 perl code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate sorting rows, function from Sort::Maker.
# As noted in "Perl Best Practices", p 164 ff.
# http://search.cpan.org/~uri/Sort-Maker-0.06/Sort/Maker.pm

use Sort::Maker;

# Set key as string beginning with second character.
my $sorter = make_sorter( 'ST', string => '/.(\w+)/' );
die "make_sorter: $@" unless $sorter;

# Omit but save fields 1 & 2, key is string beginning with fields 3-.
while (<>) {
  ( $t1, $t2, @a ) = split /\s+/;
  @b = $sorter->(@a);
  print join( " ", $t1, $t2, @b, "\n" );
}

exit(0);

-----
 Input file data1:
1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW 
3 a3 ;AS ,UY ;RF ,YT

-----
Results:
1 a1 ,AB ,AC ;AD ,EE 
2 a2 ;TR ,WE ,WW ,YT 
3 a3 ;AS ;RF ,UY ,YT

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. UNIX for Beginners Questions & Answers

How to sort a column in excel/csv file?

I have to sort the 4th column of an excel/csv file. I tried the following command sort -u --field-separator=, --numeric-sort -k 2 -n dinesh.csv > test.csv But, it's not working. Moreover, I have to do the same for more than 30 excel/csv file. So please help me to do the same. (6 Replies)
Discussion started by: dineshkumarsrk
6 Replies

3. Shell Programming and Scripting

Sort a file content using one column

Hello All, I have a file which have content as below. 03/09/2014 10:35 AM 618 Admin\rick pqr_ klm2_pog12_20140309_c.xlsx 03/10/2014 10:35 AM 618 user\test01 mplz_ fgh2_lal12_20140310_c.xlsx 03/17/2014 10:35 AM 618 Admin\vick abc_ xyz2_bc12_20140317_c.xlsx 03/18/2014 ... (2 Replies)
Discussion started by: kumar30213
2 Replies

4. UNIX for Dummies Questions & Answers

sort a unix file by 3rd column

Hi, Can anybody tell me how to sort a unix file by 3rd column and not by ltr? Please help Thanks in advance (2 Replies)
Discussion started by: vinnyvirk
2 Replies

5. UNIX for Dummies Questions & Answers

Sort text file starting at column X

Hello everyone! As the heading reads, I would like to sort the lines of a text file, starting at a specific column (i.e. skip the first X characters of each line). What I’m actually trying to sort is the md5 sums file of a directory. Every time I copy a new file to that directory, I perform... (3 Replies)
Discussion started by: iznogoud
3 Replies

6. Shell Programming and Scripting

Can't sort file by size column

Hello, I've done ls -ls >fileout1 When I do the sort command for +4 it sorts it bu group. When I do +5 it sorts it by date. But it's skipping the file size column. Example: rwxr-xr-x 1 Grueben sup 65 16 Sep 13:58 cdee How can I sort it by file size? It doesn't... (2 Replies)
Discussion started by: Grueben
2 Replies

7. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

8. Shell Programming and Scripting

Sort file based on column

Hi, My input file is $cat samp 1 siva 1 raja 2 siva 1 siva 2 raja 4 venkat i want sort this name wise...alos need to remove duplicate lines. i am using cat samp|awk '{print $2,$1}'|sort -u it showing raja 1 (3 Replies)
Discussion started by: rsivasan
3 Replies

9. Shell Programming and Scripting

Search max value in a column in a file instead of sort

Hi Everyone, 1.txt 00:00:00 0 0 0 0 0 0 0 00:00:01 0 0 0 2 1 33 2 00:00:02 5 0 0 0 0 0 0 00:00:03 0 4 0 0 0 0 0... (5 Replies)
Discussion started by: jimmy_y
5 Replies

10. Shell Programming and Scripting

Sort a particular column in a file

Dear All, Good day. Here i am facing some problem like below. file contains 12345 0001 090112 14385 0001 090112 13255 0001 090112 11345 0001 090112 .... I want to sort ascending according to the first column. What will be the shell script. (4 Replies)
Discussion started by: saifurshaon
4 Replies
Login or Register to Ask a Question