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?
- pascal.
Dear Pascal,
On 12/13/06, Pascal Nunes <pascal dot nunes at gmail dot com> wrote:
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?
sed and/or awk is better for this kind of stuff.
Please try for more details.
info sed info awk
- pascal.
With regards,
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.
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?
Now that I'm back home, here's a good method, without using multiple for loops:
#!/bin/bash num=1 for i in $(grep Hubert list | cut -f 1,3,7,8); do eval var$((num++))=$i done
The values will be stored in variables $var1 $var2 $var3 and $var4.
Sometime on Dec 13, PN cobbled together some glyphs to say:
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.
This works:
read a b c d <<EOF $( grep searchtext file | cut -f 1,3,7,8 ) EOF
On Thursday 14 December 2006 14:08, Philip Tellis wrote:
Sometime on Dec 13, PN cobbled together some glyphs to say:
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.
This works:
read a b c d <<EOF $( grep searchtext file | cut -f 1,3,7,8 ) EOF
read a b c d <<<$(grep Hubert /home/mrugesh/text/list | cut -f 1,3,5,7)
On 12/14/06, Mrugesh Karnik mrugeshkarnik@gmail.com wrote:
On Thursday 14 December 2006 14:08, Philip Tellis wrote:
Sometime on Dec 13, PN cobbled together some glyphs to say:
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.
This works:
read a b c d <<EOF $( grep searchtext file | cut -f 1,3,7,8 ) EOF
Thanks!
read a b c d <<<$(grep Hubert /home/mrugesh/text/list | cut -f 1,3,5,7)
This method worked really well. Saved a lot of time.
I had to use awk though, cause, some variable were blank and read used would not assign blank value to a variable. It would assign the value of the variblble after that... and leave a blank at the end.
- pascal.
--
Mrugesh Karnik GPG Key 0xBA6F1DA8 Public key on http://wwwkeys.pgp.net