Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Finding a word with awk or sed

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago
sam01 sam01 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 16 June 2017, 5:40 AM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you, it works with this comand :


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

just an other thing :

i might have this several times:


Code:
****************************************************************
 
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


Moderator's Comments:
Finding a word with awk or sed Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 1 Week Ago at 06:11 AM.. Reason: Changed ICODE to CODE tags.
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 3:14 AM EDT
Location: Aachen, Germany
Posts: 10,959
Thanks: 280
Thanked 3,368 Times in 3,101 Posts
Quote:
Originally Posted by sam01 View Post
.
.
.
i don't know in advance how many times i have it.
.
.
.
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.
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 27 June 2017, 4:17 AM EDT
Location: Simplicity
Posts: 3,581
Thanks: 282
Thanked 1,191 Times in 1,077 Posts
Simply add code that resets the flag if "Liste" is found. Then it will print the second "EX =" value after each "Liste"

Code:
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:

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

    #11  
Old Unix and Linux 1 Week Ago
sam01 sam01 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 16 June 2017, 5:40 AM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much
MadeInGermany.

It seems to be right. I will try it and then tell you.
Sponsored Links
    #12  
Old Unix and Linux 1 Week Ago
disedorgue disedorgue is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 26 June 2017, 12:52 PM EDT
Location: France
Posts: 453
Thanks: 37
Thanked 173 Times in 158 Posts
Hi,
Just for fun in posix sed according to gnu (--posix) :

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

Another awk version:

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

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

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

This version accept line as:

Code:
EX=FOOBAR

Regards.
Sponsored Links
    #13  
Old Unix and Linux 1 Week Ago
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 June 2017, 6:17 PM EDT
Posts: 1,633
Thanks: 46
Thanked 622 Times in 583 Posts
If you would like to try with Perl


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

Output:

Code:
CUTAFAAQ
CVGSTDA
BGVDTA

Sponsored Links
    #14  
Old Unix and Linux 1 Week Ago
sam01 sam01 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 16 June 2017, 5:40 AM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by disedorgue View Post
Hi,
Just for fun in posix sed according to gnu (--posix) :

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

Another awk version:

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

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

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

This version accept line as:

Code:
EX=FOOBAR

Regards.
Hello disedorgue and Thank's for you help (twice) Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding total count of a word. mahesh1987 Shell Programming and Scripting 6 04-02-2013 03:55 AM
Finding a specific word rpatty Shell Programming and Scripting 16 11-26-2010 10:12 AM
Getting last 50 lines after finding a word pssandeep Emergency UNIX and Linux Support 9 12-01-2009 04:57 PM
need help with finding a word in file's contents manmeet Shell Programming and Scripting 6 10-01-2008 02:21 PM
Finding a word in a file smr_rashmy Shell Programming and Scripting 10 02-13-2008 01:02 AM



All times are GMT -4. The time now is 06:34 AM.