shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] Dica do Ivan (renomear arquivos)


From: Jeiks
Subject: Re: [shell-script] Dica do Ivan (renomear arquivos)
Date: Wed, 11 Feb 2009 23:36:10 -0200

Ivan,
    já que são várias as formas de brincar com bash (ainda bem =D ),
vou mandar um outro jeitinho. Quero deixar claro que é somente pra
gente conversar e aprender com outras opiniões. :)

O script é:
____________________________
#!/bin/bash

[ $# -lt 3 ] && { echo "Utilize: $0 expr-orig expr-dest <arquivos>"; exit 1; }

ORIG=$1
DEST=$2
shift
shift
for nome in $*;do
  grep -q $ORIG <( echo ${nome}) && \
    echo mv "${nome}" "${nome%${ORIG}*}${DEST}${nome#*${ORIG}}"
# coloquei o comando acima com "echo mv" ao invés de só "mv"
# pra ficar somente nos testes e nao fazer sujeira no sistema
# :)
done
___________________________

Com ele, para alterar os arquivos mencionados no seu email, poderíamos fazer:
$ ./renomeia java c++ *.txt
mv xml-xmllint-sample-java-03-00.txt xml-xmllint-sample-c++-03-00.txt
mv xml-xmllint-sample-java-03-note.dtd.txt
xml-xmllint-sample-c++-03-note.dtd.txt
mv xml-xmllint-sample-java-03-note.xml.txt
xml-xmllint-sample-c++-03-note.xml.txt
mv xml-xmllint-sample-java-03-note.xsd.txt
xml-xmllint-sample-c++-03-note.xsd.txt

Bom, aí teríamos o problema de, no mesmo nome de arquivo, termos duas
expressões que casem com o que queremos trocar, por exemplo, o
arquivo:
c++-sample-c++-03.txt  (que possuem dois "c++")
ele faria:
$ ./renomeia.sh c++ java c++-sample-c++-03.txt
mv c++-sample-c++-03.txt c++-sample-java-sample-c++-03.txt

Mas caso isso não ocorra com os arquivos, achei interessante por não
utilizar muitos sub shells.
Gostaria de mais opiniões da lista, para ver mais derivações
interessantes e ver como shell possui muitos recursos. Ah.. também pra
ver se animam aí.... tah meio parada a lista esses dias... rs

abração a todos

2009/2/11 Ivan lopes <address@hidden>:
> * problema:
> dado os arquivos, substituir o valor 02 por 03.
>
> $ ls *.txt
> xml-xmllint-sample-validate-02-00.txt
> xml-xmllint-sample-validate-02-note.xml.txt
> xml-xmllint-sample-validate-02-note.dtd.txt
> xml-xmllint-sample-validate-02-note.xsd.txt
>
> * forma normal de resolver o problema:
> $ mv xml-xmllint-sample-validate-02-00.txt
> xml-xmllint-sample-validate-03-00.txt
> $ mv xml-xmllint-sample-validate-02-note.xml.txt
> xml-xmllint-sample-validate-03-note.xml.txt
> $ mv xml-xmllint-sample-validate-02-note.dtd.txt
> xml-xmllint-sample-validate-03-note.dtd.txt
> $ mv xml-xmllint-sample-validate-02-note.xsd.txt
> xml-xmllint-sample-validate-03-note.xsd.txt
>
> * forma ninja! ou Julhesca! ==> ( adjetivo relativo ao cara muito bom em
> Shell )
>
> hum! vejamos,
> - dupliquei a entrada : 'p'
> - substitui `02' por `03' : 's/02/03/'
> - adicionei mv na frente da linha :'s/^/mv /'
> - removi os fins de linha :'N;s/\n/ /'
>
> moleza! sed nele!
> $ ls *.txt| sed 'p; s/02/03/' | sed 'N;s/\n/ /; s/^/mv /'
> mv xml-xmllint-sample-validate-02-00.txt
> xml-xmllint-sample-validate-03-00.txt
> mv xml-xmllint-sample-validate-02-note.dtd.txt
> xml-xmllint-sample-validate-03-note.dtd.txt
> mv xml-xmllint-sample-validate-02-note.xml.txt
> xml-xmllint-sample-validate-03-note.xml.txt
> mv xml-xmllint-sample-validate-02-note.xsd.txt
> xml-xmllint-sample-validate-03-note.xsd.txt
>
> para que um texto vire uma acao! `sh' nele!
>
> $ ls *.txt| sed 'p; s/02/03/' | sed 'N;s/\n/ /; s/^/mv /'| sh
>
> !!-Atencao-!!
> o bacana desse exemplo, eh que vc pode colocar uma regex no lugar do
> `02' ... entao qualquer tipo de substituicao pode acontecer.
>
> ***transformando essa dica em algo util! <<--- achou que eu tinha esquecido!
> $ cat rename.sh
> #!/bin/bash
>
> REGEX=$1
> _STR_=$2
> # ----------------------------------------------------------
> cat -|
> sed "p; s/$REGEX/$_STR_/" | sed 'N;s/\n/ /; s/^/mv /'
>
> # ----------------------------------------------------------
> exit 0
>
> Uso:
> $ ls *.txt| ./rename.sh java c++
> mv xml-xmllint-sample-java-03-00.txt xml-xmllint-sample-c++-03-00.txt
> mv xml-xmllint-sample-java-03-note.dtd.txt
> xml-xmllint-sample-c++-03-note.dtd.txt
> mv xml-xmllint-sample-java-03-note.xml.txt
> xml-xmllint-sample-c++-03-note.xml.txt
> mv xml-xmllint-sample-java-03-note.xsd.txt
> xml-xmllint-sample-c++-03-note.xsd.txt
>
> []'s
> Ivan! :)
>
> 



-- 
Jacson R. C. Silva


reply via email to

[Prev in Thread] Current Thread [Next in Thread]