7
votes

Unix trier sur la colonne sans séparateur

J'aimerais trier un contenu de fichier avec un script UNIX en fonction d'une colonne particulière:

ex: Trier le fichier suivant sur la 3ème colonne: P>

pp0dd
aa1bb
aa3ya
ax5aa
fg7ds


1 commentaires

J'aurais fait quelque chose comme: Echo -e 'ABC \ NXYZ \ NCDE' | Perl -NPE 'S /(.)/ $ 1 / G' | Trier -k 3,3 | perl -npe 's / // g'


4 Réponses :


4
votes

Utilisez sed pour créer les colonnes avant de tri xxx


0 commentaires

2
votes
cat inputfile | perl -npe 's/(.)/ $1/g' | sort -k 3,3 | perl -npe 's/ //g'

0 commentaires

1
votes

Je tiens directement à Perl et définirais un comparateur

echo $content | perl -e 'print sort {substr($a,3,1) cmp substr($b,3,1)} <>;'


0 commentaires

10
votes
$ sort --key=1.3,1.3 inputfile
pp0dd
aa1bb
aa3ya
ax5aa
fg7ds
man page of sort:[...]-k, --key=POS1[,POS2]start a key at POS1 (origin 1), end it at POS2 (default end of line)[...]POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.With --key=1.3,1.3, you said that there only one field (the entire line) and that you're comparing the third character position of this field.

0 commentaires