Solved Assigning string to a variable in unix

April 25, 2012 at 09:01:54
Specs: Windows XP
i need to select a file name i.e. from the file 'app.txt' and i just need to select the string '.txt' from the above filename i.e. the file format.
And then i need to assign this particular selected string to one variable.
Kindly help regarding the same.. :-)

See More: Assigning string to a variable in unix

Report •

#1
April 25, 2012 at 12:47:25
Your question is a bit vague, but use command line substitution to capture the output of a command and assign it to a variable. This is for bash/ksh:

myvar=$(grep .txt app.txt)

This is for sh/csh:

# that is a gravis character (shift ~) and not a single quote:
myvar=`grep .txt app.txt`


Report •

#2
August 4, 2012 at 04:35:36
✔ Best Answer
var1=`echo app.txt| awk -F. ' { print $2 } '`

Works if there is only one dot in the filename


Report •
Related Solutions


Ask Question