| | |
AWK Extracting Multiple Lines
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I have an options file of the following format
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.
Now the problem is with the values for page_size. Although there can be multiple values for page_size, the following statement of awk
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.
Shell Scripting Syntax (Toggle Plain Text)
option1 = value1 option2 = value2 ..... ; variable number of options ..... page_size= pagesize1 ;Note there is no space between page_size and = page_size= pagesize2 page_size= pagesize3
Shell Scripting Syntax (Toggle Plain Text)
value1=$(awk '/option1/ {print $3}' $optfile) value2=$(awk '/option2/ {print $3}' $optfile)
Shell Scripting Syntax (Toggle Plain Text)
pagesize1=$(awk '/ page_size=/ {print $2}' $optfile)
So do any of you know of a way to get all three page sizes using awk?
Thanks.
バルサミコ酢やっぱいらへんで
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
I'd use arrays:
If you're using bash just "declare" opts as an array.
Shell Scripting Syntax (Toggle Plain Text)
#!/bin/ksh optfile=filename set -A opts $(awk 'BEGIN {FS="="} { if($0~ / page_size/) { print $2}}' $optfile) for i in 0 1 2 do echo ${opts[i]} done
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:
But thanks a lot really. Your reply pointed me in the correct direction.
Shell Scripting Syntax (Toggle Plain Text)
page_size=$(awk 'BEGIN {FS="="} { if($0~ /paper_size/) { print $2}}' $optfile) for paper_size in $page_size; do echo $paper_size done
バルサミコ酢やっぱいらへんで
•
•
Join Date: Sep 2006
Posts: 30
Reputation:
Solved Threads: 1
He did write:
That is:
Notice the
at the start of his code.
•
•
•
•
If you're using bash just "declare" opts as an array.
Shell Scripting Syntax (Toggle Plain Text)
declare -a opts
Shell Scripting Syntax (Toggle Plain Text)
#!/bin/ksh
•
•
Join Date: Sep 2006
Posts: 30
Reputation:
Solved Threads: 1
By the way, I wouldn't bother with awk.
Notice: I like to be able to use white space to keep it readable. The '*' matches zero or more occurances.
Shell Scripting Syntax (Toggle Plain Text)
#!/bin/ksh set -A opts $(sed -n 's/^ *paper_size *= *\(.*\)$/\1/p' optsfile) for opt in ${opts[*]} ; do echo $opt done
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
•
•
•
•
I'd use arrays:
If you're using bash just "declare" opts as an array.Shell Scripting Syntax (Toggle Plain Text)
#!/bin/ksh optfile=filename set -A opts \ $(awk 'BEGIN {FS="="} { if($0~ / page_size/) { prinf("%s ", $2}}' $optfile; echo"") for i in 0 1 2 do echo ${opts[i]} done
![]() |
Similar Threads
- print text in multiple lines in picturebox in vb6 (Visual Basic 4 / 5 / 6)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
Other Threads in the Shell Scripting Forum
- Previous Thread: sag.sh[11]: SAG: not found
- Next Thread: Scripting Please help!!
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting






