how to 'cut' a field by passing a variable
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to 'cut' a field by passing a variable




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how to 'cut' a field by passing a variable  
Ryan Gaffuri


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-16-04 10: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?





[ Post a follow-up to this message ]



    Re: how to 'cut' a field by passing a variable  
Heiner Steven


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-16-04 10: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/





[ Post a follow-up to this message ]



    Re: how to 'cut' a field by passing a variable  
Matthias Czapla


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-16-04 10: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:24 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register