AWK Extracting Multiple Lines

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

AWK Extracting Multiple Lines

 
0
  #1
Sep 11th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: AWK Extracting Multiple Lines

 
1
  #2
Sep 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: AWK Extracting Multiple Lines

 
0
  #3
Sep 15th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 30
Reputation: sut is an unknown quantity at this point 
Solved Threads: 1
sut sut is offline Offline
Light Poster

Re: AWK Extracting Multiple Lines

 
0
  #4
Sep 15th, 2006
He did write:
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: AWK Extracting Multiple Lines

 
0
  #5
Sep 15th, 2006
Yes. I saw and did that.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 30
Reputation: sut is an unknown quantity at this point 
Solved Threads: 1
sut sut is offline Offline
Light Poster

Re: AWK Extracting Multiple Lines

 
1
  #6
Sep 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: AWK Extracting Multiple Lines

 
1
  #7
Sep 15th, 2006
Originally Posted by jim mcnamara View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC