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
4 Réponses :
Utilisez sed fort> pour créer les colonnes avant de tri
cat inputfile | perl -npe 's/(.)/ $1/g' | sort -k 3,3 | perl -npe 's/ //g'
Je tiens directement à Perl et définirais un comparateur
echo $content | perl -e 'print sort {substr($a,3,1) cmp substr($b,3,1)} <>;'
$ 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.
J'aurais fait quelque chose comme:
Echo -e 'ABC \ NXYZ \ NCDE' | Perl -NPE 'S /(.)/ $ 1 / G' | Trier -k 3,3 | perl -npe 's / // g' code>