[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Dica do Ivan (renomear arquivos)
From: |
Ivan lopes |
Subject: |
Dica do Ivan (renomear arquivos) |
Date: |
Wed, 11 Feb 2009 22:44:05 -0200 |
* 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! :)