Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings in awk

Tags:

awk

I have a log file which I need to "replay" on the server.

It contains entries like this:

Request:
        query: EXEC prc_insert_customer
        @param0: 110040851
        @param1: 137463
        @param2: [email protected]
        @param3: John
        @param4: Smith
        @param5: Some address
        @param6:
        @param7:
        @param8: Some city
        @param9: GBR
        @param10: POSTCODE
        @param11: (555) 123-45-67
Response:

...

I need to convert each chunk like that into

EXEC prc_insert_customer '110040851', '137463',  ..., '(555) 123-45-67'

I tried to use awk for that:

/EXEC prc_insert_customer/ {
        str = "EXEC prc_insert_customer";
}

str && /@param/ {
        if ($1 == "@param0:")
                sep = ""
        else
                sep = ","
        str = ((str) (sep) " '"($2) ("'"))
}

/^Response/ {
        if (str)
                print str
        str = ""
}

but the output I get is:

', '(555)'DE', '', 'Some', 'GBR0851

How do I get correct output?

I use GNU Awk 4.0.0 on Fedora 17.

like image 437
Quassnoi Avatar asked Dec 07 '25 13:12

Quassnoi


1 Answers

If the log files Windows based, then you are almost certainly dealing with side-effects (affects? ;->) of the evil ^M char. It can defintely produce problems as you're describing.

 sub(/^M/,"", $0)

should help.

Thats a real Ctrl-M char and not 2 chars'^','M', produced in a vi compliant editor by pressing Ctrl-V and tne Ctrl-M.

I hope this helps.

like image 59
shellter Avatar answered Dec 11 '25 23:12

shellter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!