🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: es.comp.os.linux.programacion
8 messages
8 total messages Started by Horacio Castellini Mon, 05 Dec 2005 13:54
Como simplifico este script bash...
#1405
Author: Horacio Castellini
Date: Mon, 05 Dec 2005 13:54
57 lines
2899 bytes
Holas como simplifico este script bash... es decir como anido los 
for...que no me doy cuenta... El bash tiene funciones para procesar cadenas?

==================================================================
#!/bin/sh
i=1
for ano in 2003 2004 2005
do
for mes in January February March April 
do
        zcat $ano-$mes.txt.gz 1>/dev/null 2>/dev/null
        if [ $? != 0 ]; then
                echo "No existe $ano-$mes.txt.gz"
        else
                echo "Procesando $mes $ano"
                zcat $ano-$mes.txt.gz|egrep '^(From: .*[^<].*|Subject:.*)' >t.dat
                cat t.dat|perl -w filtro.pl >> $i.dat
                echo $i
        fi
done
cat $i.dat|./op_matriz >ad.$i.dat
i=`expr $i + 1`
echo "--------------"
for mes in February March April May 
do
        zcat $ano-$mes.txt.gz 1>/dev/null 2>/dev/null
        if [ $? != 0 ]; then
                echo "No existe $ano-$mes.txt.gz"
        else
                echo "Procesando $mes $ano"
                zcat $ano-$mes.txt.gz|egrep '^(From: .*[^<].*|Subject:.*)' >t.dat
                cat t.dat|perl -w filtro.pl >> $i.dat
                echo $i
        fi
done
cat $i.dat|./op_matriz >ad.$i.dat
i=`expr $i + 1`
echo "--------------"
for mes in March April May June 
do
        zcat $ano-$mes.txt.gz 1>/dev/null 2>/dev/null
        if [ $? != 0 ]; then
                echo "No existe $ano-$mes.txt.gz"
        else
                echo "Procesando $mes $ano"
                zcat $ano-$mes.txt.gz|egrep '^(From: .*[^<].*|Subject:.*)' >t.dat
                cat t.dat|perl -w filtro.pl >> $i.dat
                echo $i
        fi
done
cat $i.dat|./op_matriz >ad.$i.dat
i=`expr $i + 1`
echo "--------------"
etc... etc....
========================================================

Saludos Horacio....
Re: Como simplifico este script bash...
#1406
Author: Alberto Cabello Sanchez
Date: Wed, 07 Dec 2005 08:42
68 lines
2811 bytes
Horacio Castellini wrote:
> Holas como simplifico este script bash... es decir como anido los 
> for...que no me doy cuenta... El bash tiene funciones para procesar cadenas?
> 
> ==================================================================
> #!/bin/sh
> i=1
> for ano in 2003 2004 2005
> do
> for mes in January February March April 
> do
>         zcat $ano-$mes.txt.gz 1>/dev/null 2>/dev/null
>         if [ $? != 0 ]; then
>                 echo "No existe $ano-$mes.txt.gz"

Mejor algo de este estilo:
if [ -f $ano-$mes.txt.gz ]
then
	echo "Existe $ano-$mes.txt.gz"
else
	echo "No existe $ano-$mes.txt.gz"
fi

>         else
>                 echo "Procesando $mes $ano"
>                 zcat $ano-$mes.txt.gz|egrep '^(From: .*[^<].*|Subject:.*)' >t.dat

�Por qu� no usar zgrep?

>                 cat t.dat|perl -w filtro.pl >> $i.dat

�Por qu� no "perl -w filtro.pl < t.dat >> $i.dat"?

>                 echo $i
>         fi
> done
> cat $i.dat|./op_matriz >ad.$i.dat

./op_matriz < $i.dat > ad.$i.dat

> i=`expr $i + 1`
> echo "--------------"



Lo puedes hacer mejor as� (creo):

mes[0]="December"
mes[1]="January"
mes[2]="February"
mes[3]="March"
mes[4]="April"
mes[5]="May"
mes[6]="June"
mes[7]="July"
mes[8]="August"
mes[9]="September"
mes[10]="October"
mes[11]="November"


for i in 0 1 2 3 4 5 6 7 8 9 10 11
do
	for j in 0 1 2 3 4
	do
		echo ${mes[i+j]} (o mejor convirtiendo m�dulo 12)
	done
done
Re: Como simplifico este script bash...
#1407
Author: J.A. Gutierrez
Date: Wed, 07 Dec 2005 09:50
44 lines
2219 bytes
Horacio Castellini <hcaste@fonosur.com.ar> wrote:
: Holas como simplifico este script bash... es decir como anido los 
: for...que no me doy cuenta... El bash tiene funciones para procesar cadenas?

: ==================================================================
: for mes in February March April May 
: do
:         zcat $ano-$mes.txt.gz 1>/dev/null 2>/dev/null
:         if [ $? != 0 ]; then
:                 echo "No existe $ano-$mes.txt.gz"
:         else
:                 echo "Procesando $mes $ano"
:                 zcat $ano-$mes.txt.gz|egrep '^(From: .*[^<].*|Subject:.*)' >t.dat
:                 cat t.dat|perl -w filtro.pl >> $i.dat
:                 echo $i
:         fi
: done

	todo eso lo puedes simplificar como


	( for ...
		do
			...
			zcat ... | egrep ...
		done
		) | perl ... > $i.dat

	
	o incluso, probablmente

	zcat $ano-*.txt.gz | egrep ... | perl ... > $i.dat

	Pista: Muchas veces "grep | perl" puede reescribirse para
	usar unicamente perl




-- 
PGP and other useless info at      \
http://webdiis.unizar.es/~spd/      \
finger://daphne.cps.unizar.es/spd    \       Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/          \                         (Virgilio)   
Re: Como simplifico este script bash...
#1408
Author: Horacio Castellini
Date: Wed, 07 Dec 2005 13:59
11 lines
1039 bytes
Alberto Cabello Sanchez wrote:

> for i in 0 1 2 3 4 5 6 7 8 9 10 11
> do
> for j in 0 1 2 3 4
> do
> echo ${mes[i+j]} (o mejor convirtiendo m�dulo 12)
> done
> done

Gracias me diste muchas buenas pistas...
Re: Como simplifico este script bash...
#1409
Author: David Serrano (Hue-Bond)
Date: Wed, 07 Dec 2005 15:52
12 lines
1419 bytes
Alberto Cabello Sanchez, mié20051207@09:42:13(CET):
>
> mes[0]="December"
> mes[1]="January"
> mes[2]="February"
> mes[3]="March"

mes=(December January February ...)


-- 
 David Serrano
Re: Como simplifico este script bash...
#1419
Author: Alberto Cabello Sanchez
Date: Mon, 12 Dec 2005 10:53
9 lines
1523 bytes
J.A. Gutierrez wrote:

> 
> 	Pista: Muchas veces "grep | perl" puede reescribirse para
> 	usar unicamente perl


En todo caso, �no mejor (m�s sencillo, quiero decir) usar perl y hacer 
dentro los grep's?
Re: Como simplifico este script bash...
#1422
Author: J.A. Gutierrez
Date: Tue, 13 Dec 2005 08:33
20 lines
1836 bytes
Alberto Cabello Sanchez <lagallina@unex.es> wrote:
: J.A. Gutierrez wrote:

:> 
:> 	Pista: Muchas veces "grep | perl" puede reescribirse para
:> 	usar unicamente perl


: En todo caso, �no mejor (m�s sencillo, quiero decir) usar perl y hacer 
: dentro los grep's?

	depende de lo que estes haciendo concretamente, pero yo
	diria que en la mayor parte de los casos que se usa perl,
	no es necesario invocar a grep desde el script de perl.

-- 
PGP and other useless info at      \
http://webdiis.unizar.es/~spd/      \
finger://daphne.cps.unizar.es/spd    \       Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/          \                         (Virgilio)   
Re: Como simplifico este script bash...
#1428
Author: David Serrano (Hue-Bond)
Date: Fri, 16 Dec 2005 13:55
10 lines
1513 bytes
J.A. Gutierrez, mar20051213@09:33:20(CET):
>
> 	en la mayor parte de los casos que se usa perl,
> 	no es necesario invocar a grep desde el script de perl.

Sobre todo sabiendo que Perl tiene su propia función grep.


-- 
 David Serrano
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads