Finding a word with awk or sed

Hello,
in a AIX system : AIX CDRATE01 2 7 00FAB3114C00

my following commande give the result :

LISTE /tmp/RESS

 
****************************************************************
 
Liste
TYPE = XXXXXXX
 
        EX        =                                        YYYY
        VER	  =                                            S
        DATE      =                                     20160601
        HEURE     =                                       134701
 
        EX        =                                     CUTAFAAQ
        VER       =                                            X
        DATE      =                                     20160601
        HEURE     =                                       134701
 
****************************************************************

i would like to find that word : CUTAFAAQ

But this word is can change.

So how could i know this string ?

I tested this but it doesn't work :

LISTE /tmp/RESS|awk '/EX *=/ && ++flag { FS="=" ; $0=$NF ; FS=" " ; $0=$NF } flag==2 && ++flag'

or

LISTE /tmp/RESS|sed -n '1{x;s/.*/x/;x};/EX[[:space:]=]*/{x;s/x//;x;t;s/.*[[:space:]]//p;q}'

Thank's for your help

Both of these will find the word:

awk '/CUTFAAQ$/ {print}' inputfilename
# or
awk '$3 =="CUTFAAQ" && $1=="EX"  {print}' inputfilename

If this is not the case please give us examples of how CUTFAAQ exists and is not as it appears.
in your code.

Hello, i can't use the word : CUTAFAAQ because, as i said, this word is not the same each time... It often change, that's why it's quite complicated...

word="CUTAFAAQ" ; LISTE /tmp/RESS |  awk '$1=="EX" && $NF==w && $0=$NF' w="$word"

word can be set to whatever.

Thank's but i don't know the tring the in advance...
i just want to find the string, whetever it is. But before the comand :

LISTE /tmp/RESS

i can't know which will be the string. I juste his plance is always the same.

Find the second occurrence of "EX = " and print the last word

awk '($1=="EX" && $2=="=" && ++flag==2) {print $NF}'

If you don't know the word in advance, you need to define some context by which the target is identifyable, like "it's in the line starting with "EX" in the second block", or "it's on the fifth data line". You can't use "it's the last field that is 8 chars long" as this will cause false triggers...

Thank you, it works with this comand :

awk '($1=="EX" && $2=="=" && ++flag==2) {print $NF}'

just an other thing :

i might have this several times:

****************************************************************
 
Liste
TYPE = XXXXXXX
 
        EX        =                                        YYYY
        VER      =                                            S
        DATE      =                                     20160601
        HEURE     =                                       134701
 
        EX        =                                     CUTAFAAQ
        VER       =                                            X
        DATE      =                                     20160601
        HEURE     =                                       134701
 
****************************************************************
****************************************************************
 
Liste
TYPE = XXXXXXX
 
        EX        =                                        YYYY
        VER      =                                            S
        DATE      =                                     20160601
        HEURE     =                                       134701
 
        EX        =                                     CVGSTDA
        VER       =                                            X
        DATE      =                                     20160601
        HEURE     =                                       134701
 
****************************************************************
****************************************************************
 
Liste
TYPE = XXXXXXX
 
        EX        =                                        YYYY
        VER      =                                            S
        DATE      =                                     20160601
        HEURE     =                                       134701
 
        EX        =                                     BGVDTA
        VER       =                                            X
        DATE      =                                     20160601
        HEURE     =                                       134701
 
****************************************************************

i don't know in advance how many times i have it.
So in this case, i would like to get the string :

CUTAFAAQ, CVGSTDA and BGVDTA
because it appears three times. But it cas appears foor, five, six ...times

We don't either. Nor do we know your input data structure. So please keep us from guessing and supply hard facts how to identify the target values.

Simply add code that resets the flag if "Liste" is found. Then it will print the second "EX =" value after each "Liste"

awk '($1=="Liste") { flag=0 } ($1=="EX" && $2=="=" && ++flag==2) { print $NF }'

It is important that you understand the implicit if .
Within { } there is normal awk language, so you can rephrase like this:

awk '{ if ($1=="Liste") flag=0; if ($1=="EX" && $2=="=" && ++flag==2) print $NF }'

Thank you very much
MadeInGermany.

It seems to be right. I will try it and then tell you.

Hi,
Just for fun in posix sed according to gnu (--posix) :

sed -n '/TYPE/{x;s/.*/x/;x;};/ *EX *= */{x;s/x//;x;t;s/^ *EX *= *//p;}'

Another awk version:

awk '/EX *=/ && ++flag == 2 {print $NF};/TYPE/ {flag=0}'

Or (work fine under linux but i don't know under AIX) :

awk '/EX *=/ && ++flag { FS="=" ; $0=$NF ; FS=" " ; $0=$NF } flag==2 && ++flag;/TYPE/ {flag=0}'

This version accept line as:

EX=FOOBAR

Regards.

If you would like to try with Perl

perl -naF'=\s+' -le 'unless(/Liste/../HEURE/){print $F[1] if /EX/}' sam01.example

Output:

CUTAFAAQ
CVGSTDA
BGVDTA

Hello disedorgue and Thank's for you help (twice) :smiley: