- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Awk - help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 01:16 AM
04-17-2007 01:16 AM
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
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 02:15 AM
04-17-2007 02:15 AM
Re: Awk - help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 05:23 PM
04-17-2007 05:23 PM
Re: Awk - help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 08:05 PM
04-17-2007 08:05 PM
Re: Awk - help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 09:23 PM
04-17-2007 09:23 PM
Solutionwhat 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 <
/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 10:49 PM
04-17-2007 10:49 PM
Re: Awk - help
I modified your first script to get it working..:-)Thanks a lot!!!!
And closing this thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2007 10:50 PM
04-17-2007 10:50 PM