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 06-15-2017   -   Original Discussion by sam01
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; 06-15-2017 at 07:11 AM.. Reason: Changed ICODE to CODE tags.
Sponsored Links
    #9  
Old Unix and Linux 06-15-2017   -   Original Discussion by sam01
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 1:17 PM EST
Location: Aachen, Germany
Posts: 11,652
Thanks: 323
Thanked 3,620 Times in 3,325 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 06-15-2017   -   Original Discussion by sam01
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 November 2017, 10:59 AM EST
Location: Simplicity
Posts: 3,831
Thanks: 319
Thanked 1,284 Times in 1,162 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 06-15-2017   -   Original Discussion by sam01
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 06-15-2017   -   Original Discussion by sam01
disedorgue disedorgue is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 21 November 2017, 1:20 PM EST
Location: France
Posts: 463
Thanks: 37
Thanked 179 Times in 162 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 06-15-2017   -   Original Discussion by sam01
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 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 06-16-2017   -   Original Discussion by sam01
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 04:55 AM
Finding a specific word rpatty Shell Programming and Scripting 16 11-26-2010 11:12 AM
Getting last 50 lines after finding a word pssandeep Emergency UNIX and Linux Support 9 12-01-2009 05:57 PM
need help with finding a word in file's contents manmeet Shell Programming and Scripting 6 10-01-2008 03:21 PM
Finding a word in a file smr_rashmy Shell Programming and Scripting 10 02-13-2008 02:02 AM



All times are GMT -4. The time now is 02:28 PM.