1828480 Members
2979 Online
109978 Solutions
New Discussion

Re: Awk - help

 
SOLVED
Go to solution
Senthil Prabu.S_1
Trusted Contributor

Awk - help

Hi,
I have a script to match a list of patterns defined in array and do some operation. The flow is "array elements are passed into a while loop, where pattern matching on individual elements using awk". It works fine when array element have single "/" like "/var", but awk fails when array element have two slaces like "/global/TspOam".

I knew special characters can be given in "[ ]" to match the pattern, but it didnot help me.

My script looks like this;
#!/usr/bin/ksh -x
set -A filesys /var

i=0
while [ $i -lt ${#filesys[@]} ]
do
position_params=`awk ${filesys[$i]}[\^\/]/'{print}' test.txt | grep -v '^[ ]*#'`
echo $position_params
((i=i+1))
done
........it goes on

When array is populated with elements like "/global/TspOam", awk fails inside the while loop.

Can anyone help me....

Note:
Running on solaris 10 OS...

Thanks,
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
6 REPLIES 6
Peter Nikitka
Honored Contributor

Re: Awk - help

Hi,

on Solaris boxes I generally do not recommend to use plain 'awk' - it's normally an ancient version:
ls -goi /usr/bin/*awk
26691 -r-xr-xr-x 2 89128 Jan 8 19:30 /usr/bin/awk
26694 -r-xr-xr-x 1 126964 Jan 8 19:30 /usr/bin/nawk
26691 -r-xr-xr-x 2 89128 Jan 8 19:30 /usr/bin/oawk

Just try first
nawk (= /usr/bin/nawk)
then
/usr/xpg4/bin/awk

Next - which is NOT Solaris specific stuff:
- there's no need for grep - awk can do it by itself much more efficient. I guess you want to drop lines commented out by this.
- your awk-command is really doubtful, because match operator and your pattern itself get mixed up. Having no 'test.txt' I can only guess what result you want to get, however.

Try this (
I use the match operator and set a variable 'pat' to clarify the pattern):

#!/usr/bin/ksh -x
set -A filesys /var /usr/bin

typeset -i i=0
while [ $i -lt ${#filesys[@]} ]
do
position_params=$(nawk -v fsys=${filesys[$i]} 'BEGIN {pat="^"fsys"[^/]*"}
/^[ ]*#/ {next}
$0 ~ pat {print}' test.txt)
echo $position_params
((i=i+1))
done


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Senthil Prabu.S_1
Trusted Contributor

Re: Awk - help

Hi,
My test.txt will have lines containing patterns /var, /globa/Tsp...etc.[hard-coded in array] when these are matched, then they should be stored in the variable "position_params".

But your script didn't store the patterns in the variable position_params. It outputs nothing.


Can you help....

Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Arturo Galbiati
Esteemed Contributor

Re: Awk - help

Hi Prabu,
why awk and not simply grep?
This runs fine on my HP-UX11i:
set -A filesys /var /globa/Tsp
typeset -i i=0
while [ $i -lt ${#filesys[@]} ]
do
position_params=$(grep ${filesys[$i]} test.txt)
echo $i $position_params
((i=i+1))
done

0 /var
1 /globa/Tsp

cat test.txt
/var
/globa/Tsp

HTH,
Art
Peter Nikitka
Honored Contributor
Solution

Re: Awk - help

Hi,

what do you mean:
>>
hardcoded in array
<<

The following code - slightly modified to my prior solution - produces this output:
/usr/bin udir

#!/usr/bin/ksh
set -A filesys /var /usr/bin
typeset -i i=0
cat >test.txt </var/tmp tmpdir
/usr/local hostdir
#/var sysdir
/usr udir
/usr/bin udir
EOF

while [ $i -lt ${#filesys[@]} ]
do
position_params=$(nawk -v fsys=${filesys[$i]} 'BEGIN {pat="^"fsys"$"}
/^[ ]*#/ {next}
$1 ~ pat {print}' test.txt)
[ -n "$position_params" ] && echo $position_params
((i=i+1))
done

Now let us know, what are your exact input and output request.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Senthil Prabu.S_1
Trusted Contributor

Re: Awk - help

Hello Peter,
I modified your first script to get it working..:-)Thanks a lot!!!!


And closing this thread.
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Senthil Prabu.S_1
Trusted Contributor

Re: Awk - help

Got the solution, closing it.
One man's "magic" is another man's engineering. "Supernatural" is a null word.