Substring in Shell Script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Substring in Shell Script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-09-2007
smartbuddy smartbuddy is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 27 August 2007, 6:15 AM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Substring in Shell Script

Hi

I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file.

Eg:

I got a file like, Readme.txt and has the following name value pairs

input1 : /homes/input1/
input2 : /homes/input2/
...
...

When I give the parameter input1, the value /homes/input1/ has to returned from the file and get stored in a variable.

Thanks in advance.
Sponsored Links
    #2  
Old 08-09-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
#!/bin/ksh

inpStr=$1;
inpFile="Readme.txt"
myVar=$(grep -x $inpStr $inpFile | cut -d':' -f2)

Sponsored Links
    #3  
Old 08-09-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
store=`awk -F ":" '/input1/ { print $2 }' filename`

    #4  
Old 08-17-2007
smartbuddy smartbuddy is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 27 August 2007, 6:15 AM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

Quote:
Originally Posted by lorcan View Post
Code:
#!/bin/ksh

inpStr=$1;
inpFile="Readme.txt"
myVar=$(grep -x $inpStr $inpFile | cut -d':' -f2)

Thanks lorcan... Its working fine...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with Korn Shell script for substring printing kraljic Shell Programming and Scripting 6 10-14-2011 12:43 PM
using substring in shell script kmanivan82 Shell Programming and Scripting 24 10-23-2008 11:50 AM
Substring in shell script jyotib Shell Programming and Scripting 5 01-16-2008 06:58 PM
Substring function in UNIX shell script csrazdan UNIX for Dummies Questions & Answers 5 08-12-2006 04:07 PM
Substring in C shell script? dinodash Shell Programming and Scripting 0 03-20-2005 12:59 PM



All times are GMT -4. The time now is 06:20 PM.