Unix Programming - how to 'cut' a field by passing a variable

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > how to 'cut' a field by passing a variable





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com