script liste de fichiers

script liste de fichiers - Codes et scripts - Linux et OS Alternatifs

Marsh Posté le 16-04-2007 à 22:28:28    

'lut,

 

je suis en train d'écrire des petits scripts afin d'améliorer mon utilisation des commandes de base.
j'arrive à un nouveau step: awk

 

en ayant ça dans un répertoire:

cfd@seagull /common/temp $ ls
f1.txt  f2.mp3  f3.avi  f4.mp3  f5.txt

 

voilà ce à quoi je voudrais arriver:

cfd@seagull /common/temp $ <...>
1 avi, 2 mp3 and 2 txt file(s)

 

(en gros, c'est pour taper l'output dans conky)

 

pour l'instant j'ai ça:

cfd@seagull /common/temp $ ls | grep \\. | cut -d. -f2 | sort | uniq -c | awk '{ printf "%s %s, ", $1, $2;} END { print ""}'
1 avi, 2 mp3, 2 txt,

 

mais j'ai un peu de mal

  • à trouver la dernière ligne courante mangée par awk afin de placer le 'and' et le 'file(s)' (le script awk)

ls | grep \\. | cut -d. -f2 | sort | uniq -c | awk '{ out= out ";" $1 " " $2} END { print out; split(out, segments,";" ); if(length(segments)==0)exit; printf "%s", segments[2]; if(length(segments)>2) { i=3; for (; i<length(segments); i++) printf ", %s", segments[i]; printf " and %s ", segments[i];} print "file(s)" }'
1 avi, 2 mp3 and 2 txt file(s)

 
  • à ce que ça marche si j'ai des fichiers contenant des . dans le nom en plus de la séparation avec l'extension (le cut qui est limite)

ls -X1 $1 | awk '{ split($0, s, "." ); print s[length(s)]; }' | uniq -c | awk '{ out= out ";" $1 " " $2} END { split(out, segments,";" ); if(length(segments)==0)exit; printf "%s", segments[2]; if(length(segments)>2) { i=3; for (; i<length(segments); i++) printf ", %s", segments[i]; printf " and %s ", segments[i];} print "file(s)" }'

 

Qq'un pour simplifier le premier awk avec sed ? car je n'y arrive pô :sweat:


Message édité par TBone le 16-04-2007 à 23:59:14

---------------
As the plane took off, the pilot turned to the co-pilot and said, “Have you ever flown solo?” Co-pilot: No. Typically I fly much higher than this.
Reply

Marsh Posté le 16-04-2007 à 22:28:28   

Reply

Marsh Posté le 17-04-2007 à 10:49:25    

le simplifier par sed, c'est possible mais pas maintenable donc, j'arrête :)
 
par contre, il y a moyen de le simplifier un peu:
 

awk '{ split($0, s, "." ); print s[length(s)]; }'


devient

awk '{ n=split($0, s, "." ); print s[n]; }'


 
'oilà :)
 
donc au bout du compte j'ai dans listFile.sh :

Code :
  1. #!/bin/bash
  2. temp=`ls -X1 $1 | awk '{ n=split($0, s, "." ); print s[n]; }' | uniq -c | awk '{ out= out ";" $1 " " $2} END { split(out, segments,";" ); if(length(segments)==0)exit; printf "%s", segments[2]; if(length(segments)>2) { i=3; for (; i<length(segments); i++) printf ", %s", segments[i]; printf " and %s ", segments[i];} print "file(s)" }'`
  3. echo temp


 
:)


---------------
As the plane took off, the pilot turned to the co-pilot and said, “Have you ever flown solo?” Co-pilot: No. Typically I fly much higher than this.
Reply

Marsh Posté le 17-04-2007 à 11:13:08    

A titre purement informatif, moi je fais ça comme ça :

 

#!/bin/sh
##########################################################################
# Shellscript: extcounter
# Date       : 2006-12-14
# Category   : File Utilities
##########################################################################
# Description
#   Count and sort the number of files by extension
##########################################################################

 


if [ "$1" = "-r" ]; then
    SEARCH="find . -name '*.*' -print"
else
    SEARCH="ls -d *.*"
fi

 

FILTER="| sed 's/.*\.//g' | sort | uniq -c | sort -n"

 

eval "$SEARCH" "$FILTER"

 

:hello:


Message édité par Xavier_OM le 25-03-2011 à 17:07:30

---------------
Il y a autant d'atomes d'oxygène dans une molécule d'eau que d'étoiles dans le système solaire.
Reply

Marsh Posté le 17-04-2007 à 11:17:28    

intéressant ça. je teste ce soâr :jap:


---------------
As the plane took off, the pilot turned to the co-pilot and said, “Have you ever flown solo?” Co-pilot: No. Typically I fly much higher than this.
Reply

Marsh Posté le 17-04-2007 à 22:31:39    

en effet, je préfère mettre "sed 's/.*\.//g'" en lieu et place de mon premier awk, ça me paraît moins long à exécuter (même si ça trace vu le peu de fichiers ;) )

 

edit> tout compte fait, les commandes unix c'est comme le SQL, plus on en fait plus on trouve des astuces :)

 

:jap:


Message édité par TBone le 17-04-2007 à 22:33:42

---------------
As the plane took off, the pilot turned to the co-pilot and said, “Have you ever flown solo?” Co-pilot: No. Typically I fly much higher than this.
Reply

Marsh Posté le 20-04-2007 à 13:07:28    

un update histoire de gérer la casse des caractères  :sarcastic:  

#!/bin/bash
temp=`ls -X1 $1 | sed 's/.*\.//g' | awk '{ print tolower($0); }' | sort | uniq -c | awk '{ out= out ";" $1 " " $2} END { split(out, segments,";" ); if(length(segments)==0)exit; printf "%s", segments[2]; if(length(segments)>2) { i=3; for (; i<length(segments); i++) printf ", %s", segments[i]; printf " and %s ", segments[i];} print "file(s)" }'`
 
echo $temp


---------------
As the plane took off, the pilot turned to the co-pilot and said, “Have you ever flown solo?” Co-pilot: No. Typically I fly much higher than this.
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed