Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attempting to pull rows out of a file and then insert into a variable

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
#!/bin/ksh -p

Example:

File: Test

Users Misc
----- ----

Data: Jack 784
Dirk 2345
Cindy 0987
Noone 3412

I use this to only pull what I want to see...

cat Test|awk '{print $1}' > /tmp/test.out

Now from here I want to be able to look at /tmp/test.out and pull each name from the file and put those into a case statement. Can this be done? I think it can be done using awks getline, but I am unable to figure this out from the man pages. Anyone have any ideas? Thanks in advance.
 
Hi,
In CSH this would be simply

set names = ( `awk '{print $1}' test` )

then
$names[1] would be "Jack"
$names[2] would be "Dirk"

and so on.

I don't know if this can be KSH.
 
In ksh you could probably do it something like this:

for name in `cat test`
do
....case statements....
done

If you're only wanting the names from the test file, do a:

cat test | tr -s ' ' ' ' | cut -f1 -d' ' > newtest

first, to get just the names from the file in question into another file, which you can then plug into the script above. HTH.

 
Here is the issue, this script is going to be put onto 5000 unix servers and I do not know what is contained within the test file, so I have to find a way to get those values into variables. I found something close to what I want:

sed 1q test | awk -F: '{print $1}' -- This would just pull Jack out which I could then assign a variable to, but this:

sed 2q test | awk -F: '{print $1}' -- would pull both Jack and Dirk out in this case. I only want to pull Dirk out to assign a variable to.
 
Not exactly sure what you want. You could try using perl, and use a hash keyed by the user, for example if your data file looks like:
Code:
Jack:784
Dirk:2345
Cindy:0987
Then you would need to do something like:
Code:
open( DATA, "data" ) or die;
chomp( %id = map { split /:/ } <DATA> );
foreach $user ( sort keys %id ) {
    print &quot;$user has id $id{$user}\n&quot;;
}
Line 2 above simply makes a hash of 'user => id' pairs.

Hope this makes sense with what you are trying to achieve. Cheers, NEIL :)
 
Maybe this will help:

#!/bin/ksh -p

# Here I am pulling the second column from test.doc

cat /all/test.doc | awk '{print $2}' > /tmp/test.out


# Now I want to look at /tmp/test.out and assign each row
# (value) a variable. I can do this for the first value
# using this... X=$(sed 1q test.out | awk -F: '{print $1}')
# The problem is I cannot do this for the second variable,
# third variable, forth variable, etc.

So my question is how can I do this? Is it possible to pull a row with a value in it, (that you do not know what it is), and assign a variable to it?
 
The shell construct that Ken describes seems to do exactly what you want, each item (row) pulled out of the file will get assigned to a variable ($name in this case)
[tt]
#!/usr/bin/ksh

for name in $(awk '{print $1}' Test)
do
[tab]print $name
done
[/tt]

Is that what you mean? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top