Arguments sys.argv [1] .split () as string?

2

I'm stuck in a script. I try to expose you to see if you can help me.

I define the following before entering the loop.

cliente, fecha, hora, state, nombre, result, most_repeated = sys.argv[1].split()

timestamp = str(fecha)+' '+' '+str(hora)
CLIENTE 08-01-2018 08:44:00 1 Prueba1 ALERT 4000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS

The execution is as follows:

./script.py "CLIENTE 08-01-2018 08:44:00 1 nombre de la alerta que sea 5000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

By putting spaces between the parameters this takes them as different parameters as we all know.

I want it:

specifically the fields

" name of the alert that is " and " 192.168.1.113 ==> & 8.8.8.8:53 ==> DNS "

Take them as a single parameter.

I've been formatting it with "% s" But when more words come out in the name of the alert it will not work.

    
asked by Zero22 09.01.2018 в 10:16
source

3 answers

0

If as you say only nombre and most_repeated contain spaces and the structure of most_repeated has as separator " ==> " we can always approach it combining str.split and str.rsplit , next to the parameter maxsplit .

arg = "CLIENTE 08-01-2018 08:44:00 1 nombre de la alerta que sea 5000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

cliente, fecha, hora, state, rest = arg.split(maxsplit=4)
rest = rest.rsplit(" ==>> ")
nombre, result, mr_0 = rest[0].rsplit(maxsplit=2)
most_repeated = " ==>> ".join([mr_0] + rest[1:])

print(cliente)
print(fecha)
print(hora)
print(state)
print(nombre)
print(result)
print(most_repeated)

With which we obtain each parameter as we see in the output:

CLIENTE
08-01-2018
08:44:00
1
nombre de la alerta que sea
5000
192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS

I do not think it's a very elegant solution but in reality the structure of the string is terrible to be parsed as arguments. It will work without problems provided that cliente , fecha , hora , state and result do not contain spaces inside and that most_repeated is of the form algo ==> algo or a string without spaces. Another option is to use regular expressions, which can give us more flexibility.

    
answered by 09.01.2018 / 15:40
source
0

I think you could use another element of the blank space to separate the parameters. Let me explain, using for example the separator '**' in this way: sys.argv[1].split('**') and then that the execution of the script is ./script.py "CLIENTE**08-01-2018 08:44:00**1**nombre de la alerta que sea**5000**192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

    
answered by 09.01.2018 в 11:35
0

Simply to add the option with regular expressions, surely it can be improved.

import re

txt = "CLIENTE 08-01-2018 08:44:00 1 nombre de la alerta que sea 5000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

pattern = "^(.*) (\d{2}-\d{2}-\d{4}) (\d{2}:\d{2}:\d{2}) (\d{1,5}) (.*) (\d{1,5}) ((?:[0-9]+(?:\.[0-9]+){3}(?::[0-9]+)?) ==>> (?:[0-9]+(?:\.[0-9]+){3}(?::[0-9]+)?) ==>> .*$)$"

p = re.compile(pattern)
m = p.match(txt)

if m:
  print("Matching existoso")
  print("cliente      : {0}".format(m.group(1)))
  print("fecha        : {0}".format(m.group(2)))
  print("hora         : {0}".format(m.group(3)))
  print("state        : {0}".format(m.group(4)))
  print("nombre       : {0}".format(m.group(5)))
  print("result       : {0}".format(m.group(6)))
  print("most_repeated: {0}".format(m.group(7)))

Exit:

Matching existoso
cliente      : CLIENTE
fecha        : 08-01-2018
hora         : 08:44:00
state        : 1
nombre       : nombre de la alerta que sea
result       : 5000
most_repeated: 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS
    
answered by 09.01.2018 в 16:39