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: Fernando Gottlieb
Subject: Re: [shell-script] Dica do Ivan (renomear arquivos)
Date: Thu, 12 Feb 2009 14:23:59 -0200

Caramba!!!
Não sabia deste potencial do SED!!!
Realmente há muito que aprender!!
Onde posso encontrar material para desenvolver este tipo de solução?
Esta semana adquiri os livros Expressões Regulares e Shell Script
Profissional (Aurélio Marinho Jargas) e
Programação Shell Script Linux (Júlio Cezar Neves), porém ainda não
tive tempo de lê-los.
Mas esta solução com o SED me deixou de cabelos em pé!!

Abraços à todos!

Fernando A. Gottlieb

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! :)
>
> 


reply via email to

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