- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Easy shell scripting 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
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
09-30-2004 02:10 AM
09-30-2004 02:10 AM
field1 field11
field222 field22222222
All fields separated by a single space.
I want to run through this script and set a var equal to each of the two fields for a record, then do something else.
EX:
var1=field1
var2=field11
Just looking for the most efficient way to do this.
TIA and points for all responses.
Sean
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:24 AM
09-30-2004 02:24 AM
Solutioncat file|while read field1 field2 rest
do
var1=field1
var2=field2
.
.
.
done
See www.shelldorado.com for scripting
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:32 AM
09-30-2004 02:32 AM
Re: Easy shell scripting question
while read var1 var2
do
# something
done < input_file
(If you dont trust input to be nice)
You can also read as:
while THE_LINE=$(line)
do
echo $THE_LINE | read var1 var2
# something
done < input_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:35 AM
09-30-2004 02:35 AM
Re: Easy shell scripting question
Would something like this work for you?
field1="$(cat
field11="$(cat
for field1 in `$(cat
do
for field11 in `$(cat
do
echo $field1 $field11
done
done
Just a couple of ideas. If I had a sample file to test with, I couple probably come up with something a bit more extravagant :)
Dwyane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:37 AM
09-30-2004 02:37 AM
Re: Easy shell scripting question
field1="$(cat
echo $field1
field11="$(cat
echo $field11"
got ahead of myself a little bit :)
Dwyane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:41 AM
09-30-2004 02:41 AM
Re: Easy shell scripting question
num=1
cat
do
echo var$num=$a
let num="num + 1"
echo var$num=$b
done
...assuming that you want the var_num ascending
Regards
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:43 AM
09-30-2004 02:43 AM
Re: Easy shell scripting question
cat yourfile | while read line
do
read var1 var2 rest
echo "$var1 $var2"
done
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:47 AM
09-30-2004 02:47 AM
Re: Easy shell scripting question
i think a while read should be the most efficient way to do this.
Something like:
while read var1 var2
do
# do something here
done < yourinputfile
This will run through all lines of yourinputfile and for every line you can do something and use var1 for the first field and var2 for the second field. As in your example in the first loop var1 would be "field1" and var2 would be "field11" (without the " of course). In the second loop var1=field222 and var2=field22222222 and so on.
Hope this helps.
Regards Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:49 AM
09-30-2004 02:49 AM
Re: Easy shell scripting question
Never mind.
Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:53 AM
09-30-2004 02:53 AM
Re: Easy shell scripting question
while read line; do
var1=$(echo $line | awk '{ print $1 }')
var2=$(echo $line | awk '{ print $2 }')
done <
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:00 AM
09-30-2004 03:00 AM
Re: Easy shell scripting question
if you store first field and second filed on var1 , var2 only last record's information only available there.
So we can avoid this with array simply as,
# Array index
index=0
while read line; do
var1[$index]=$(echo $line | awk '{ print $1 }')
var2[$index]=$(echo $line | awk '{ print $2 }')
let index=index+1
done <
so that
for first record it is saved as,
echo ${var1[1]} ${var2[1]}
and nth record as,
echo ${var1[n]} ${var2[n]}
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:05 AM
09-30-2004 03:05 AM
Re: Easy shell scripting question
sorry i've missed one line :-(
num=1
cat
do
echo var$num=$a
let num="num + 1"
echo var$num=$b
let num="num + 1"
done
Regards
Franky