LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Grep \ Shell Script \ Parsing (https://www.linuxquestions.org/questions/programming-9/grep-%5C-shell-script-%5C-parsing-4175429264/)

drd0spt 09-27-2012 04:53 AM

Grep \ Shell Script \ Parsing
 
Hi i know there is some other threads about grep and parsing but i tryed most of the other ways describe on the other questions and didnt work for me, i would like to parse a openssl log to a .csv and by that make the usual ; to pars the columns .

output to parse :

Signature Algorithm: sha1WithRSAEncryption

Issuer: C=PT, L=Jersey, O=UFO - UFO Solutions,
CN=UFO Software ROOT Primary CA/emailAddress=ufo@wtf.us

Validity
Not Before: Jan 24 11:10:13 2011 GMT
Not After : Jan 21 11:10:13 2021 GMT

Subject: C=US, L=Jersey, O=UFO - UFO Solutions,OU=CER, CN=UFO PSS Intermediate CA/emailAddress=ufo@wtf.us

Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)

I want to parse the data in front of "Signature Algorithm :" , the dates in "Not Before:" and "Not After", and the values in front of "C=", "L=" , "CN=", "O=" , "OU= ", "EmaiAddress=" . And finally the Public key algorithm : , and RSA public key value the " (2048 bit) "

to the >> example.csv

The desired output would be :

sha1WithRSAEncryption, Jan 24 11:10:13 2011 GMT, Jan 21 11:10:13 2021 GMT, PT, Jersey, UFO, etc.


thanks in advance.

business_kid 09-27-2012 05:10 AM

Search for a download - why reinvent the wheel?

For something as involved as you are asking, grep & bash is a bit cumbersome. There is awk, and sed which might thrash it out better for you.

That said, if you do go at this, there is an option for you to use perl's regular expression stuff which is a bit tidier and more definite than posix, in my experience.
man perlre is your friend

drd0spt 09-27-2012 05:13 AM

I have this :

Quote:

#!/bin/bash
#v.0.1

wrd=$(grep -oP '(?<==)\w*' ex.txt)


for w in $wrd
do
$w";" >> resultado.csv
done
But that just dont parse the ":" values or in thi case the dates

pixellany 09-27-2012 05:24 AM

drd*;

I think you mean the data AFTER the keywords---for example, when it finds "Signature Algorithm:", it would return: "sha1WithRSAEncryption"

To be clear, please post the desired output I'm guessing that it would be:
"sha1WithRSAEncryption, Jan 24 11:10:13 2011 GMT, Jan 21 11:10:13 2021 GMT, etc."

drd0spt 09-27-2012 05:29 AM

That is exactly what i want pixellany :)

the desired output would be :

"sha1WithRSAEncryption, Jan 24 11:10:13 2011 GMT, Jan 21 11:10:13 2021 GMT, PT, Jersey, UFO, etc."

pixellany 09-28-2012 04:34 AM

Here is a start (written as a script):
Code:

#!/bin/bash
sed -n -r -e 's/Signature Algorithm: (.*)$/\1/p' \
-e 's/Not Before: (.*)$/\1/p' \
-e 's/Not After: (.*)$/\1/p' \
-e '/Issuer:/s/.*C=([^,]*),.*L=([^,]*),.*O=([^-]*)-.*/\1 \2 \3/p' < filename

It does not yet pick up every item, and they are not yet in order.

I am QUITE SURE there is an easier way to do this......;)


All times are GMT -5. The time now is 06:29 PM.