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
Substr in shell script gjithin Shell Programming and Scripting 7 05-10-2008 04:40 PM
substr() thru awk Korn Shell Script sbryant Shell Programming and Scripting 4 01-24-2008 11:16 AM
how to assign sql output data to shell script variable kattics Shell Programming and Scripting 1 07-07-2006 03:57 PM
Trouble using substr function with Bourne shell script E2004 Shell Programming and Scripting 4 09-29-2005 10:57 AM
return variable from PL/SQL procedure to shell ap_gore79 Shell Programming and Scripting 4 06-03-2004 04:49 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 07-22-2008
ttunell ttunell is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code:

x="O1164885.DAT"
y=`ls -ltr *${x}|awk '{print substr($0,3)}'`
echo ${x}|awk '{print substr($0,3)}'
echo "y="$y

I can echo it to the screen just fine but I can't figure out how to store it back into a script variable.

Any help would be appreciated.

Thanks!
  #2 (permalink)  
Old 07-22-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
which line are you truing to put in a var?


Im guessing thats what you wanted:
somevar=`echo ${x}|awk '{print substr($0,3)}'`
  #3 (permalink)  
Old 07-22-2008
ttunell ttunell is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
That works great! Many thanks!
  #4 (permalink)  
Old 07-23-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Another method using no pipes:

Code:
somevar=`expr substr "$x" 1 3`
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 06:53 PM.


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