| Author |
how to 'cut' a field by passing a variable
|
|
| Ryan Gaffuri 2004-06-16, 5:57 pm |
| The following returns the entire line, I just want one field...
CUT_FIELD=2
RETURN_VALUE=`grep ^${GREP_NAME} $FILE_NAME|cut -f${CUT_FIELD} -d:`
echo $RETURN_VALUE
data looks like this:
GREP_NAME HELLO GOODBYE 1
In this case I just want to return 'GOODBYE', but in other cases I may
want to return HELLO or 1?
| |
| Heiner Steven 2004-06-16, 5:57 pm |
| Ryan Gaffuri wrote:
> The following returns the entire line, I just want one field...
>
> CUT_FIELD=2
> RETURN_VALUE=`grep ^${GREP_NAME} $FILE_NAME|cut -f${CUT_FIELD} -d:`
> echo $RETURN_VALUE
>
> data looks like this:
>
> GREP_NAME HELLO GOODBYE 1
>
> In this case I just want to return 'GOODBYE', but in other cases I may
> want to return HELLO or 1?
RETURN_VALUE=`awk "/^$GREP_NAME/ { print \$$CUT_FIELD }" "$FILE_NAME"`
Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
| |
| Matthias Czapla 2004-06-16, 5:57 pm |
| Ryan Gaffuri wrote:
> The following returns the entire line, I just want one field...
>
> CUT_FIELD=2
> RETURN_VALUE=`grep ^${GREP_NAME} $FILE_NAME|cut -f${CUT_FIELD} -d:`
> echo $RETURN_VALUE
>
> data looks like this:
>
> GREP_NAME HELLO GOODBYE 1
The fields are separated by whitespace but the in the variable
RETURN_VALUE you specify a colon as the separator (option "-d:").
Change this to "-d ' '" and it should work.
Regards
Matthias
|
|
|
|