For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2006 > get a word from a line matched by the preceding word..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author get a word from a line matched by the preceding word..
reuben12

2006-04-17, 6:56 pm

I have a mulit line of output that i use grep to get the single line

The single line is of the format

one two, three(three) four TYPE value ....

I need to get the word following "TYPE" , ie , "value"

In perl , it would be something like below

if( $_ =~ m/CONFIGURATION ([A-Za-z]+) /i )
{
print OFILE $1 ;
}

I tried the below but it did not work

#!/bin/ksh
value=`test.exe | grep sync | grep -v grep | grep $1 | awk ' $0 ~
/CONFIGURATION \w*/ { print $1}'`

Please let me know the correct way to do this.

Thank you.

Bob Harris

2006-04-17, 9:56 pm

In article
<1145289529.550511.108430@t31g2000cwb.googlegroups.com>,
"reuben12" <praveenaugustus@gmail.com> wrote:

> I have a mulit line of output that i use grep to get the single line
>
> The single line is of the format
>
> one two, three(three) four TYPE value ....
>
> I need to get the word following "TYPE" , ie , "value"
>
> In perl , it would be something like below
>
> if( $_ =~ m/CONFIGURATION ([A-Za-z]+) /i )
> {
> print OFILE $1 ;
> }
>
> I tried the below but it did not work
>
> #!/bin/ksh
> value=`test.exe | grep sync | grep -v grep | grep $1 | awk ' $0 ~
> /CONFIGURATION \w*/ { print $1}'`
>
> Please let me know the correct way to do this.
>
> Thank you.


#!/bin/sh
value=`echo "sync atype CONFIGURATION thisword def" | awk -v
TYPE="$1" '
/sync/ && $0 ~ TYPE {
match($0,/CONFIGURATION/)
$0 = substr($0,RSTART+RLENGTH)
print $1
}
'`
echo $value

I got rid of all the grep commands, and did the record selection
using awk.

My example uses an echo of a single line, as I did not have your
test.exe and I needed something to test with. Replace the echo
with your test.exe.

One of your greps was matching the shell argument $1, so I passed
that into the awk script via the -v TYPE="$1" and then selected
records that included /sync/ another one of your greps,
and $0 ~ TYPE.

I used match() to locate "CONFIGURATION", then used substr() to
extract all the text following CONFIGURATION, assigned it to $0
which would automatically reparse the line into $1, $2, $3, etc...
words, and printed $1 which should be the first word following
CONFIGURATION.

Is that about what you were looking for?

Then again, if you know how to do it in Perl, why not use Perl?

Bob Harris
Ed Morton

2006-04-17, 9:56 pm

reuben12 wrote:
> I have a mulit line of output that i use grep to get the single line
>
> The single line is of the format
>
> one two, three(three) four TYPE value ....
>
> I need to get the word following "TYPE" , ie , "value"
>
> In perl , it would be something like below
>
> if( $_ =~ m/CONFIGURATION ([A-Za-z]+) /i )
> {
> print OFILE $1 ;
> }
>
> I tried the below but it did not work
>
> #!/bin/ksh
> value=`test.exe | grep sync | grep -v grep | grep $1 | awk ' $0 ~
> /CONFIGURATION \w*/ { print $1}'`
>
> Please let me know the correct way to do this.
>
> Thank you.
>


I THINK what you're saying is that you want to look for the field that
follows the string "TYPE" on each line that follows a line that contains
the string ""CONFIGURATION". If that's the case, all you MAY need is:

awk 'f{sub(/.* TYPE */,"");sub(/ .*/,"");print;f=0}/CONFIGURATION/{f=1}'
file

depending on what your input really looks like. It's also possible that
you could just set your RS to some string that'll accomodate your
multi-line fields. Without sampel input it's hard to say....

Regards,

Ed.
reuben12

2006-04-19, 6:57 pm

# ./test2.sh temp
awk: syntax error near line 2
awk: bailing out near line 2
../test2.sh: ^J /sync/ && $0 ~ TYPE {^J
match($0,/CONFIGURATION/)^J $0 = substr($0,RSTART+RLENGTH)^J
print $1^J }^J: not found

# cat test2.sh
#!/bin/sh
value=`echo "sync atype CONFIGURATION thisword def" | awk -v
TYPE="$1" '
/sync/ && $0 ~ TYPE {
match($0,/CONFIGURATION/)
$0 = substr($0,RSTART+RLENGTH)
print $1
}
'`
echo $value
# which awk
/bin/awk
# uname -a
SunOS hlr900 5.9 Generic_118558-24 sun4u sparc SUNW,Netra-240
#


I'm getting errors while executing your sample script.
Can you please let me know how to debug awk. How can i get more
detailed error messages?

Ed Morton

2006-04-19, 6:57 pm

reuben12 wrote:
> # ./test2.sh temp
> awk: syntax error near line 2
> awk: bailing out near line 2
> ./test2.sh: ^J /sync/ && $0 ~ TYPE {^J
> match($0,/CONFIGURATION/)^J $0 = substr($0,RSTART+RLENGTH)^J
> print $1^J }^J: not found
>
> # cat test2.sh
> #!/bin/sh
> value=`echo "sync atype CONFIGURATION thisword def" | awk -v
> TYPE="$1" '
> /sync/ && $0 ~ TYPE {
> match($0,/CONFIGURATION/)
> $0 = substr($0,RSTART+RLENGTH)
> print $1
> }
> '`
> echo $value
> # which awk
> /bin/awk
> # uname -a
> SunOS hlr900 5.9 Generic_118558-24 sun4u sparc SUNW,Netra-240
> #
>
>
> I'm getting errors while executing your sample script.
> Can you please let me know how to debug awk. How can i get more
> detailed error messages?
>


Your using old, broken awk. Use gawk, nawk, or /usr/xpg4/bin/awk.

Ed.
reuben12

2006-04-19, 6:57 pm

Ed,
Sorry for the confusion due to my unclear post.

The input is going to be in one line.

In that one line of input , i need to get the word following
CONFIGURATION.
That is i need to search for one word and get the word following that
in one line of inpout that is obtained by the execution of an
executable.

Ed Morton

2006-04-19, 6:57 pm

reuben12 wrote:

> Ed,
> Sorry for the confusion due to my unclear post.
>
> The input is going to be in one line.
>
> In that one line of input , i need to get the word following
> CONFIGURATION.
> That is i need to search for one word and get the word following that
> in one line of inpout that is obtained by the execution of an
> executable.



How's this:

$ echo "abc CONFIGURATION def ghi" |
gawk '{print
gensub(/. *[[:space:]]CONFIGURATION[[:space:]]*([^
[:space:]]+).*/,"\\1","")}'
def

Regards,

Ed.
reuben12

2006-04-19, 6:57 pm

Thanks !!
I used "nawk" and it worked.
A search on google showed that awk on solaris is very old
and suggested to use nawk,gawk.. for match() function.

surferelf

2006-04-27, 6:57 pm

In article <1d-dnUbHjYOiENvZRVn-pg@comcast.com>, Ed Morton wrote:
>
> How's this:
>
> $ echo "abc CONFIGURATION def ghi" |
> gawk '{print
> gensub(/. *[[:space:]]CONFIGURATION[[:space:]]*([^
[:space:]]+).*/,"\\1","")}'
> def


What would be wrong with the following?

gawk '{for(i=1;i<=NF;i++){if($i ~ /CONFIGURATION/){print $(i+1)}}}'
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com