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

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

Identificação de parâmetros numéricos com awk


From: Breno Moiana
Subject: Identificação de parâmetros numéricos com awk
Date: Thu, 19 Nov 2009 17:05:15 -0200

Boa tarde, pessoal.

Tenho um arquivo com uma série de comandos com seus parametros, no
seguinte formato:

> cat datafile
command PARNUM=0001 BLABLA=wewew BLIBLI=laala
command PARAM1=oneone PARNUM=0002
command noparm
command PARAM1=oneone PARAM2=twotwo PARNUM="0003"
command PARNUM=4 PARAM=lalal
command PAR=lalala PARNUM="5" PARA=lelele
command PARA=lalal PARA2=leelel PARNUM=6
command PARA=lala PARNUM=07 PRA=pra
command PARA=lalal PARNUM="008"
command PARNUM=0000000009

Estou refazendo um script que identifica e valida os valores de
PARNUM, que tem que ser numérico, de 0 a 9.
O jeito que está feito hoje é bem deselegante, e eu queria arrumar isso.
Ele força que PARNUM seja 0000, 0001, 0002, 0003, etc.

Eu gostaria que aceitasse 1, 02, "0004", 03, etc.

Eis o script atual, já limpo e comentado:

{
#1.Print full command line for debugging purposes
print "========>" $0

#2.if PARNUM= parameter is present, test it
if (match($0,"PARNUM="))
{
#3.Select the value assigned to PARNUM
VALUE_PAR = substr($0, RSTART +7 , 4)

#4.print the assigned value for debugging
print VALUE_PAR

#5.test the assigned value against list
if ( VALUE_PAR != "0000" && VALUE_PAR != "0001" && VALUE_PAR != "0002"
&& VALUE_PAR != "0003" && VALUE_PAR != "0004" && VALUE_PAR != "0005"
&& VALUE_PAR != "0006" && VALUE_PAR != "0007" && VALUE_PAR != "0008"
&& VALUE_PAR != "0009" )
print "Error in value: [" VALUE_PAR "]"
else
print "Value [" VALUE_PAR "] accepted"

}

}

Eu não gosto do passo 3, onde acho que deve ter um jeito de assignar o
valor sem ser com uma largura fixa, nem do passo 5, que deveria poder
ser substituido por uma regex. Pensei em 0*[0-9], mas nao encontrei
como encaixar isso dentro do if do awk

O meu output com o arquivo de teste é o seguinte (note que as linhas
que nao conformam com 0001 sem aspas geram erro)


========>command PARNUM=0001 BLABLA=wewew BLIBLI=laala
0001
Value [0001] accepted
========>command PARAM1=oneone PARNUM=0002
0002
Value [0002] accepted
========>command noparm
========>command PARAM1=oneone PARAM2=twotwo PARNUM="0003"
"000
Error in value: ["000]
========>command PARNUM=4 PARAM=lalal
4 PA
Error in value: [4 PA]
========>command PAR=lalala PARNUM="5" PARA=lelele
"5"
Error in value: ["5" ]
========>command PARA=lalal PARA2=leelel PARNUM=6
6
Error in value: [6]
========>command PARA=lala PARNUM=07 PRA=pra
07 P
Error in value: [07 P]
========>command PARA=lalal PARNUM="008"
"008
Error in value: ["008]
========>command PARNUM=0000000009
0000
Value [0000] accepted



Quaisquer idéias/sugestoes sao, como sempre, muito bem-vindas.

Valeu!


Breno.
--
"If you're gonna eat three times a day to the day that you die, why
not be good at it?

It will save you money, it will more than likely make  you
healthier... and you'll be happier!"
Jamie Oliver


reply via email to

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