- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- shell script question
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
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
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
тАО09-19-2002 07:58 AM
тАО09-19-2002 07:58 AM
shell script question
I am parsing lines in a file delimited by colons.
Can someone give me a clever line in awk or POSIX that will assign $VAR1=field1 $VAR2=field2, etc?
I need to loop through each line and do work on $VAR1 and $VAR2.
I can only pass files to the cut command, not each line that I parse with my "for" statement.
I'm not that familiar with awk.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:04 AM
тАО09-19-2002 08:04 AM
Re: shell script question
man on awk will show that you can define the field delimiter with the -F option, and inside of awk you can set anything equel to anything.
If you want to get external variables, look at the awk man pages for the -v option, which is variable assignment prior to running awk.
Not really sure what you mean though by setting $VAR2=field2 and what your trying to accomplish though... perhaps telling us what these are will help get more accurate answers.
I.E. awk has $0, $1, etc... where is the $var and field coming from?
Regards,
Shannon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:09 AM
тАО09-19-2002 08:09 AM
Re: shell script question
awk -F':' '{ print $1}' /etc/passwd
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:09 AM
тАО09-19-2002 08:09 AM
Re: shell script question
INFILE=/tmp/myfile
awk -F':' '{print $1, $2, $3, $4}' < ${INFILE} | while read A B C D
do
echo "A=${A}"
echo "B=${B}"
echo "C=${C}"
echo "D=${D}"
done
If less than four fields are found on a given input record (a line) then only those found will have non-null values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:09 AM
тАО09-19-2002 08:09 AM
Re: shell script question
#!/bin/ksh
IFS=:
exec 0while read -r Name PW Uid Gid Gecos Home Shell
do
VAR1=$Name
VAR2=$Pass
VAR3=$Uid
....
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:11 AM
тАО09-19-2002 08:11 AM
Re: shell script question
Something like:
# V=b
# echo "a:one\nb:two\nc:three"|awk -F: -v V=$V '$1~V {print $2}'
...would echo "two" from the record "b".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:12 AM
тАО09-19-2002 08:12 AM
Re: shell script question
You can do this directly is the shell as follows:-
If reading directly from a file...
#!/usr/bin/sh
IFS=:
{
while read var1 var2 rest
do
done
} <
unset IFS
If reading from a pipe...
#!/usr/bin/sh
IFS=:
while read var1 var2 rest
do
done
}
unset IFS
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:13 AM
тАО09-19-2002 08:13 AM
Re: shell script question
do
echo $var1 $var2
done
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:20 AM
тАО09-19-2002 08:20 AM
Re: shell script question
perl -F: -nae 'print $F[0], "\n"' /etc/passwd
The "-F" sets the separator character.
Each line gets split into the F array, numbered 0..n, so instead of printing the variable out, you can modify it within the perl script.
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 09:11 AM
тАО09-19-2002 09:11 AM
Re: shell script question
Your answer was the easiest!! Exactly what I needed.
I could have done this in Perl, but its clumsy when you have a bunch of shell commands, so I didn't use it.
Thanks everyone!!!
- Allan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2002 08:21 PM
тАО09-19-2002 08:21 PM
Re: shell script question
oravp1:8940:221:/usr/bin/csh
vq1adm:9277:224:/usr/bin/csh
oravq1:9276:221:/usr/bin/csh
rbillard:6498:10000:/usr/bin/sh
sanman:11574:20:/usr/bin/ksh
and this script:
for lines in $(do
VAR1=`echo $lines|cut -f 1 -d:`
VAR2=`echo $lines|cut -f 2 -d:`
VAR3=`echo $lines|cut -f 3 -d:`
VAR4=`echo $lines|cut -f 4 -d:`
echo "VAR1=$VAR1"
echo "VAR2=$VAR2"
echo "VAR3=$VAR3"
echo "VAR4=$VAR4"
done
and got this output:
VAR1=oravp1
VAR2=8940
VAR3=221
VAR4=/usr/bin/csh
VAR1=vq1adm
VAR2=9277
VAR3=224
VAR4=/usr/bin/csh
VAR1=oravq1
VAR2=9276
VAR3=221
VAR4=/usr/bin/csh
VAR1=rbillard
VAR2=6498
VAR3=10000
VAR4=/usr/bin/sh
VAR1=sanman
VAR2=11574
VAR3=20
VAR4=/usr/bin/ksh
You could replace the cut -fx -d: with awk -F: '{print $x}'. Either will work.
Keit