943,962 Members | Top Members by Rank

Ad:
Sep 11th, 2006
0

AWK Extracting Multiple Lines

Expand Post »
I have an options file of the following format
Shell Scripting Syntax (Toggle Plain Text)
  1. option1 = value1
  2. option2 = value2
  3. ..... ; variable number of options
  4. .....
  5. page_size= pagesize1 ;Note there is no space between page_size and =
  6. page_size= pagesize2
  7. page_size= pagesize3
During a regression test I want to extract the values for these options. Since the keywords "option1", option2, ... are different from each other, I can use awk to extract the values of value1, value2 as follows.

Shell Scripting Syntax (Toggle Plain Text)
  1. value1=$(awk '/option1/ {print $3}' $optfile)
  2. value2=$(awk '/option2/ {print $3}' $optfile)
Now the problem is with the values for page_size. Although there can be multiple values for page_size, the following statement of awk
Shell Scripting Syntax (Toggle Plain Text)
  1. pagesize1=$(awk '/ page_size=/ {print $2}' $optfile)
gives me only the corresponding value for the first match of page_size.

So do any of you know of a way to get all three page sizes using awk?
Thanks.
Similar Threads
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Sep 12th, 2006
1

Re: AWK Extracting Multiple Lines

I'd use arrays:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. optfile=filename
  3. set -A opts $(awk 'BEGIN {FS="="} { if($0~ / page_size/) { print $2}}' $optfile)
  4. for i in 0 1 2
  5. do
  6. echo ${opts[i]}
  7. done
If you're using bash just "declare" opts as an array.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Sep 15th, 2006
0

Re: AWK Extracting Multiple Lines

I tried it in my cygwin bash shell. Maybe it is something unexpected in cygwin, but all the matches were stored in opts[0]. So ultimately I could do this without using arrays. :eek:
Shell Scripting Syntax (Toggle Plain Text)
  1. page_size=$(awk 'BEGIN {FS="="} { if($0~ /paper_size/) { print $2}}' $optfile)
  2. for paper_size in $page_size; do
  3. echo $paper_size
  4. done
But thanks a lot really. Your reply pointed me in the correct direction.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Sep 15th, 2006
0

Re: AWK Extracting Multiple Lines

He did write:
Quote ...
If you're using bash just "declare" opts as an array.
That is:
Shell Scripting Syntax (Toggle Plain Text)
  1. declare -a opts
Notice the
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
at the start of his code.
sut
Reputation Points: 20
Solved Threads: 1
Light Poster
sut is offline Offline
30 posts
since Sep 2006
Sep 15th, 2006
0

Re: AWK Extracting Multiple Lines

Yes. I saw and did that.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Sep 15th, 2006
1

Re: AWK Extracting Multiple Lines

By the way, I wouldn't bother with awk.
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. set -A opts $(sed -n 's/^ *paper_size *= *\(.*\)$/\1/p' optsfile)
  3. for opt in ${opts[*]} ; do
  4. echo $opt
  5. done
Notice: I like to be able to use white space to keep it readable. The '*' matches zero or more occurances.
sut
Reputation Points: 20
Solved Threads: 1
Light Poster
sut is offline Offline
30 posts
since Sep 2006
Sep 15th, 2006
1

Re: AWK Extracting Multiple Lines

I'd use arrays:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. optfile=filename
  3. set -A opts \
  4. $(awk 'BEGIN {FS="="} { if($0~ / page_size/) { prinf("%s ", $2}}' $optfile; echo"")
  5. for i in 0 1 2
  6. do
  7. echo ${opts[i]}
  8. done
If you're using bash just "declare" opts as an array.
I changed it so awk puts it all on the same line.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: sag.sh[11]: SAG: not found
Next Thread in Shell Scripting Forum Timeline: Scripting Please help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC