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

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

Re: [shell-script] Re: trabalhando com linhas de arquivo ( e o sed -i ?)


From: mop
Subject: Re: [shell-script] Re: trabalhando com linhas de arquivo ( e o sed -i ?)
Date: Tue, 27 Nov 2007 21:50:36 -0200

Exatamente Flávio, o sed cria outro arquivo, só que isso fica invisível,
ou transparente para o usuário, pois ele recebe tudo pronto, só que isso
dependendo do caso pode não atender. Segui os seus passos e fui um pouco além.
Ficou um pouco longo o relato, mas quero deixar aqui registrado para a
posteridade. Segue abaixo da sua mensagem.

Caso haja outras idéias, eu pelo menos, estou sempre interessado em aprender.


------------------------------------------------------------------
>From: "Flavio Junior
>Date: Tue, 27 Nov 2007 16:15:39 -0200
>
>Hm..
>
>Se eu entendi o que tu tentou demonstrar ali é que, o sed -i na real
>cria uma nova entrada de diretorio no sistema de arquivos né ?
>
>O que gera um novo i-node.
>
>O proprio "vim" usa a tecnica no sed.. o que se mostrou mais rapido (o
>sed) nos meus testes mesmo. Mas o "ed" (oh, o ed...) pode fazer o que
>voce quer:
>
>[root@bugsbunny ~]# ls -i teste.txt; cat teste.txt
>2097218 teste.txt
>teste
>[root@bugsbunny ~]# ed -s teste.txt <<< $'s/e/3/g
>w
>q'
>[root@bugsbunny ~]# ls -i teste.txt; cat teste.txt
>2097218 teste.txt
>t3st3
>[root@bugsbunny ~]# vim +':s/3/e/g' +':wq' teste.txt
>[root@bugsbunny ~]# ls -i teste.txt; cat teste.txt
>2097220 teste.txt
>teste
>[root@bugsbunny ~]#
>
>
>--
>
>
>Flávio do Carmo Júnior



-------------------------------------------------------------------------

CONCLUSAO: "ed" OK, arquivo binário e inode

sed____________inode_____tamanho______md5sum______________________
original_______755189____95412________17849d2c502626e2595874681e4364fa
alterado_______755189____95712
revertido______755189____95412________17849d2c502626e2595874681e4364fa

Funcionou perfeitamente em um arquivo binário.
Uma cópia do sed foi usada como cobaia para dilaceração. :)
Subtituindo uma string por outra de tamanho diferente e na sequência
revertendo o processo.
Além da ótima postagem do Flavio Junior \o/, posteriormente olhei também esta
página: http://www.cs.utk.edu/~cs300/Sed/lecture.html




-------  DETALHAMENTO DO TESTE COM O ed ----------------------------
atenção: o programa aqui é o "ed", a cópia do sed é o arquivo manipulado

=========
Não tinha o ed, tive de baixar da GNU, configure, make, e usei dalí mesmo.
Copiando e examinando a vítima binária:

web@k7/tmp/ed-0.8$ cp -i /bin/sed .
web@k7/tmp/ed-0.8$ ls -li sed
755189 -rwxr-xr-x 1 web  ppp 95412 2007-11-27 20:28 sed*
web@k7/tmp/ed-0.8$ md5 sed
17849d2c502626e2595874681e4364fa  sed



========
Criando a função subst, só para sentir a praticidade: subst str1 srt2 file

web@k7/tmp/ed-0.8$ \
subst(){ [ -e $3 ]&&/tmp/ed-0.8/ed -s $3<<<"1,\$s/$1/$2/g"$'\n'w;}




=========
Listando a str1 (command) no arquivo:

web@k7/tmp/ed-0.8$ strings sed|grep command
                 add the script to the commands to be executed
                 add the contents of script-file to the commands to be executed
                 specify the desired line-wrap length for the `l' command
extra characters after command
missing command
command only uses one address
unterminated `s' command
unterminated `y' command
multiple `p' options to `s' command
multiple `g' options to `s' command
multiple number options to `s' command
number option to `s' command may not be zero
strings for `y' command are different lengths
unknown command: `%c'
invalid reference \%d on `s' command's RHS



========
Usando a função criada:

web@k7/tmp/ed-0.8$ subst command '0123456789ABCDEFGHIJKLMOP:)' sed
Newline appended




========
Inspecionando o resultado:

web@k7/tmp/ed-0.8$ strings sed|grep MOP
                 add the script to the 0123456789ABCDEFGHIJKLMOP:)s to be execut
ed
                 add the contents of script-file to the 0123456789ABCDEFGHIJKLMO
P:)s to be executed
                 specify the desired line-wrap length for the `l' 0123456789ABCD
EFGHIJKLMOP:)
extra characters after 0123456789ABCDEFGHIJKLMOP:)
missing 0123456789ABCDEFGHIJKLMOP:)
0123456789ABCDEFGHIJKLMOP:) only uses one address
unterminated `s' 0123456789ABCDEFGHIJKLMOP:)
unterminated `y' 0123456789ABCDEFGHIJKLMOP:)
multiple `p' options to `s' 0123456789ABCDEFGHIJKLMOP:)
multiple `g' options to `s' 0123456789ABCDEFGHIJKLMOP:)
multiple number options to `s' 0123456789ABCDEFGHIJKLMOP:)
number option to `s' 0123456789ABCDEFGHIJKLMOP:) may not be zero
strings for `y' 0123456789ABCDEFGHIJKLMOP:) are different lengths
unknown 0123456789ABCDEFGHIJKLMOP:): `%c'
invalid reference \%d on `s' 0123456789ABCDEFGHIJKLMOP:)'s RHS

web@k7/tmp/ed-0.8$ ls -li sed
755189 -rwxr-xr-x 1 web  ppp 95712 2007-11-27 20:30 sed*



=========
Fazendo a substituição inversa:

web@k7/tmp/ed-0.8$ subst '0123456789ABCDEFGHIJKLMOP:)' command sed
Newline appended
web@k7/tmp/ed-0.8$ ls -li sed
755189 -rwxr-xr-x 1 web  ppp 95412 2007-11-27 20:33 sed*
web@k7/tmp/ed-0.8$ md5 sed
17849d2c502626e2595874681e4364fa  sed


==========
Apesar da inserção de linha pelo ed tudo terminou bem, pois o próprio ed a
retira ao final.


reply via email to

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