linuxawk

get a string/phrase with awk from 1 line


We are using a script which it returns as a result the following

I'm trying to get from awk the phrase that says CONSULTA CORRECTA, as you can see everything is in just one line, no spaces or comma. So I got no idea how to get that phrase and set it up in a variable.

I used

const=$(awk '/CONSULTA CORRECTA/ ' output)

and

const=$(awk '/^CONSULTA CORRECTA/ ' output)

But either the whole phrase comes together with the rest of the info I dont need, or the variable is empty.

Any help appreciated.


Solution

  • Since this is a simple substitution on a single string it's a better candidate for a sed script than an awk script:

    $ sed -n 's:.*<mensaje>\(.*\)</mensaje>.*:\1:p' file
    CONSULTA DE DEUDA CORRECTA.
    
    $ const=$(sed -n 's:.*<mensaje>\(.*\)</mensaje>.*:\1:p' file)
    $ echo "$const"
    CONSULTA DE DEUDA CORRECTA.
    

    If you really want to use awk though then with GNU awk for the 3rd arg to match():

    $ awk 'match($0,"<mensaje>(.*)</mensaje>",a){print a[1]}' file
    CONSULTA DE DEUDA CORRECTA.