On Wednesday 13 December 2006 21:22, Pascal Nunes wrote:
hi,
I am writing a shell script where I retrive values from a text file using cut. I want to save these into variables.
The script is something like this...
grep searchtext file | cut -f 1,3,7,8
I want to save the values returned by cut into 4 different variables.
Can anyone suggest a method to save these values into multiple variables?
Is there any better method to do this?
An ugly method:
variable=($(grep searchtext file | cut -f 1,3,7,8))
This will save the values in an array $variable
You can then refer to the values from the various positions or use a simple for loop to put the values from the array into different variables. Then again, this assumes that the output for each field is a single word. The array elements will be separated by spaces.
Please provide details as to the data being processed.
Another way I can think or right now is to use multiple for loops.
There might be an easier method, but I don't have time right now. I have to run. These are they first two methods that popped into my head immediately.
Then again, skip all this and consider sed and awk.
HTH.