Bash - Redondance dans un fichier

Résolu
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   -  
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   - 8 nov. 2011 à 21:31
Bonjour à tous!

Ma question est très simple: Comment faire en bash pour supprimer les lignes redondantes dans un fichier?

J'utilise la fonction
mv -iv $* >> $HOME/backup/.backup
mais lorsque le fichier existe déjà et que je demande à le remplacer, il me rajoute quand même une ligne. Alors qu'il faudrait voir si la ligne existe déjà dans le fichier.

Merci d'avance!

A voir également:

3 réponses

koma_666 Messages postés 429 Date d'inscription   Statut Membre Dernière intervention   34
 
en changeant le flux de redirection peut être ?
un simple au lieu d'un double.

mv -iv $* > $HOME/backup/.backup
au lieu de
mv -iv $* >> $HOME/backup/.backup
0
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   708
 
Sa ne pourra pas marcher, c'est pour faire une genre de corbeille en bash pour l'université, des fichiers peuvent être déplacés a des moments différents.
0
zipe31 Messages postés 36402 Date d'inscription   Statut Contributeur Dernière intervention   6 427
 
Salut,

Comment faire en bash pour supprimer les lignes redondantes dans un fichier?

sort -u fichier_redondant > fichier_épuré


;-))
0
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   708
 
J'essaye sa dans 2h quand je suis en TP! Merci!
0
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   708
 
Sa fonctionne! maintenant j'ai un nouveau soucis avec la commande mv:
Quand je lance la commande
bash ./MSc/Bash/backup ablah.txt blah.txt

Elle execute la fonction
mv -iv $PWD/$* $HOME/backup/ >> $HOME/backup/.backup

mais elle insrit dans le fichier $HOME/backup/.backup ceci:
'blah.txt' -> '/home8/p09267388/backup/blah.txt'
'/home8/p09267388/ablah.txt' -> '/home8/p09267388/backup/ablah.txt'


comment faire pour qu'elle inscrive ceci?
'/home8/p09267388/blah.txt' -> '/home8/p09267388/backup/blah.txt'
'/home8/p09267388/ablah.txt' -> '/home8/p09267388/backup/ablah.txt'
0
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   708
 
c'est bon j'utilise
for ARG in "$@"
do
    if mv -iv $PWD/$ARG $HOME/backup/ >> $HOME/backup/.backup
    then 
        sort -u $HOME/backup/.backup > $HOME/backup/.backupTemp
        mv $HOME/backup/.backupTemp $HOME/backup/.backup
    fi
done
0
Remad Messages postés 1662 Date d'inscription   Statut Membre Dernière intervention   708
 
Voyez-vous une optimisation possible dans mon code?
#!/bin/bash 
#Pierre cybard - p09267388 

if [ -d $HOME/backup ] #test if a backup directory exists 
then 
  if [ $# -eq 0 ] #if no parameters 
  then 
    echo -n "No parameters given. Do you want to backup the current folder? Y/N: " 
    read answer 
    if [ $answer == "Y" ] || [ $answer == "y" ] || [ $answer == "Yes" ] || [ $answer == "YES" ] || [ $answer == "yes" ] #Yes = backup the current folder / other = do nothing 
    then 
      chmod -R 700 $HOME/backup #restore the write permission on the folder 
      mv -iv $PWD/ $HOME/backup/ >> $HOME/backup/.backup #move the current folder, add a line on the .backup file 
      sort -u $HOME/backup/.backup > $HOME/backup/.backupTemp #sorting by name, avoiding redundancy, on a temporary file 
      mv $HOME/backup/.backupTemp $HOME/backup/.backup #move the temporary file to the .backup file 
      chmod -R 500 $HOME/backup #restore the limited permissions 
    fi 
  else #if parameters 
    chmod -R 700 $HOME/backup #restore the write permission on the folder 
    for ARG in "$@" #for every parameters 
    do 
      mv -iv $PWD/$ARG $HOME/backup/ >> $HOME/backup/.backup #move the file/folder and add a line on the .backup file 
    done 
    sort -u $HOME/backup/.backup > $HOME/backup/.backupTemp #sorting by name, avoiding redundancy, on a temporary file 
    mv $HOME/backup/.backupTemp $HOME/backup/.backup #move the temporary file to the .backup file 
    chmod -R 500 $HOME/backup #restore the limited permissions 
  fi 
else #if backup directory do not exists 
    echo "The backup folder do not exist. Please use the initialise function." 
fi
0