jeudi 18 octobre 2012

Some stuff....

SED : (merci nico)

to remove, with a sed command, every thing before the dot :

echo " aaa.bbb" | sed 's/^.*\.//g'
bbb

^ = from the line beginning
. = one caracter

.* (dot with *) every caracters


Python : 

I have the following file, which contains server names, and dates.

root@test # cat file
server_a
09192012
09292012
09302012
server_b

 04022012
04082012
04262012
10072012
...


The date is american format, so i need to convert it in europeen format, so
reverse chain 2 by 2  (convert date from mmddyyyy to ddmmyyyy)

the simplest python command to do that is :

>>> x=06242012 
>>>x=x[2:4:]+x[0:2:]+x[4:8::]
>>> x
'24062012'


As i need to use it on the command line, to process a verrryy large file, i built a simple python file :

root@test #cat conv.py

#!/bin/python
# -*-coding:utf-8 -*

import sys
for line in sys.stdin:
        line=line.strip("\r\n") # need to remove trailing \n because my var is not numbers if there is a \n
        if line.isdigit():          # we only rework lines that are numbers
                line=line[2:4:]+line[0:2:]+line[4:8:]
        print line


root@test #cat file | ./conv.py 

server_a
19092012
29092012
30092012
server_b
22022012
18042012
26042012
07102012
...

 job's done...

same stuff with perl (one liner!) (merci Guigui)

root@test #echo 09192012 | perl -ne "s/(\d\d)(\d\d)(\d\d\d\d)/\2\/\1\/\3/;print"
19/09/2012

Aucun commentaire:

Enregistrer un commentaire