The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
fetch substring from html code scarfake Shell Programming and Scripting 2 05-29-2008 09:55 PM
How to fetch data from a text file in Unix shikhakaul Shell Programming and Scripting 4 01-25-2008 11:20 AM
How To Fetch Time From Putside Source On Unix Platform? pesty UNIX for Advanced & Expert Users 2 05-11-2007 07:06 AM
Fetch the field from file and add to new file. maestrohero Shell Programming and Scripting 2 04-18-2007 09:00 PM
field delimiter with a space or more uphamtn UNIX for Dummies Questions & Answers 3 05-15-2003 04:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-27-2008
autosys_nm autosys_nm is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
Unix code to fetch the first field till space in a variable

Hi,

I want to get the value of the first field till space in a variable.

e.g x=323 /test/personel/logs/File1 [After 3 space is there]

I want to get the first field till space i.e 323 in another variable ,lets say y.

echo $x|cut -d' ' -f1 gives 323
but when I'm trying y=`echo $x|cut -d' ' -f1 its giving error.

Please help.
  #2 (permalink)  
Old 08-27-2008
yongitz yongitz is offline
Registered User
  
 

Join Date: Apr 2008
Location: Philippines
Posts: 68
Hi! to be sure, since your value has a space in between them, enclosed them with a double quotes, like this:
Code:
x="323 /test/personel/logs/File1"
In your command substitution(using backticks), I don't know if it's a typo or what? But it should be written as this,
Code:
y=`echo $x|cut -d' ' -f1`
  #3 (permalink)  
Old 08-27-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
y=`echo $x|awk '{print $1}'`
  #4 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
It's both safer and more efficient to use the shell's built-in variable substitution syntax.

Code:
y=${x% *}
To be sure, echo $x without double-quoting the variable name $x will wreck the whitespace in the variable's value.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0