Finding a word with awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a word with awk or sed
# 8  
Old 06-15-2017
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:
Mod Comment 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.
# 9  
Old 06-15-2017
Quote:
Originally Posted by sam01
.
.
.
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.
# 10  
Old 06-15-2017
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 06-15-2017
Thank you very much
MadeInGermany.

It seems to be right. I will try it and then tell you.
# 12  
Old 06-15-2017
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.
# 13  
Old 06-15-2017
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

# 14  
Old 06-16-2017
Quote:
Originally Posted by disedorgue
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) Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retrieve information Text/Word from HTML code using awk/sed

awk/sed newbie here. I have a HTML file and from that file and I would like to retrieve a text word. <font face=arial size=-1><li><a href=/value_for_clients/Tokyo/abc_process.txt>abc</a> NDK Version: 4.0 </li> <font face=arial size=-1><li><a... (6 Replies)
Discussion started by: sk2code
6 Replies

2. Shell Programming and Scripting

sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log: 98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw;... (6 Replies)
Discussion started by: erlanq
6 Replies

3. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

4. Shell Programming and Scripting

making the first character of word using uppercase using awk and sed

I want to make the first character of some words to be uppercase. I have a file like the one below. uid,givenname,sn,cn,mail,telephonenumber mattj,matt,johnson,matt johnson,mattj@gmail.com markv,mark,vennet,matt s vennet,markv@gmail.com mikea,mike,austi,mike austin,mike@gmail.com I want... (3 Replies)
Discussion started by: matt12
3 Replies

5. Shell Programming and Scripting

sed or awk to print 2nd last word

Hi, I have a file which has the following /usr/new/xyz/abc /us1/neb/yxr/def /usr/bin/cloud1/fgh /net/bin1/txt1/kdq I want to do something like this /usr/new/xyz/abc xyz /us1/neb/yxr/def yxr /usr/bin/cloud1/fgh cloud1 /net/bin1/txt1/kdq txt1 I need to add the 2nd last word to the... (3 Replies)
Discussion started by: matbrow
3 Replies

6. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

8. Shell Programming and Scripting

Getting the first word using sed,awk or perl

Input: root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mail:x:8: Output: root daemon bin sys adm tty (8 Replies)
Discussion started by: cola
8 Replies

9. Shell Programming and Scripting

Sed or awk cut all lines after word

Hi, sorry for newbie question :confused: can't find how to cut ? from 1000 2000 word some text1.... 100 200 300 word some text2.... 10 20 30 abc word some text3.... to some text1.... some text2.... some text3.... (7 Replies)
Discussion started by: Trump
7 Replies

10. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question