dirname, save cut portion to variable, clean up


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting dirname, save cut portion to variable, clean up
# 1  
Old 10-06-2008
dirname, save cut portion to variable, clean up

Hi guys, last cry for help for today. I appreciate the help so far.

ok so I have a program that dumps a path into my script as a variable ($1)
This path is an example
/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3

So in order to search thetvdb.com for a show, I need to extract the show name to pass to the other portions you guys have been helping me with so far today. The show name in the path will always be correct and there may often be spaces. I know I can truncate the last dir from the path using dirname but in this case I need to keep the show name.

the beginning of the path:
/home/xbmc/sab_downloads/video/tv

will always be the same with no exceptions however there may not always be anything trailing the name of the show, for example:
/home/xbmc/sab_downloads/video/tv/example

How can I truncate the name of the show in this environment and save it to a variable?

Thanks in advance!
# 2  
Old 10-06-2008
if you have this
Code:
/home/xbmc/sab_downloads/video/tv/example

and you want
Code:
/home/xbmc/sab_downloads/video/tv

in some variable use
Code:
 
var=`dirname /home/xbmc/sab_downloads/video/tv/example`

or
if you have
Code:
/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3

and first part remains same try this
Code:
 
var=`echo "/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3"|sed 's/\(\/home\/xbmc\/sab_downloads\/video\/tv\/\)\(.*\)/\1/g' `

# 3  
Old 10-06-2008

The shell has parameter expansion that can do what you want; there is no need for an external command:

Code:
path="/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3"
showseason=${path#/home/xbmc/sab_downloads/video/tv/}
show=${showseason%/*}

# 4  
Old 10-06-2008
Quote:
Originally Posted by vidyadhar85
if you have this
Code:
/home/xbmc/sab_downloads/video/tv/example

and you want
Code:
/home/xbmc/sab_downloads/video/tv

in some variable use
Code:
 
var=`dirname /home/xbmc/sab_downloads/video/tv/example`

or
if you have
Code:
/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3

and first part remains same try this
Code:
 
var=`echo "/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3"|sed 's/\(\/home\/xbmc\/sab_downloads\/video\/tv\/\)\(.*\)/\1/g' `

hey vid

the only part that will always remain the same is
Code:
/home/xbmc/sab_downloads/video/tv

after that the path changes, the first dir after "tv" is what I want to save to a variable


So if I have
Code:
/home/xbmc/sab_downloads/video/tv/test example

the part I want to save to a variable is
Code:
test example

If I have
Code:
/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3

I can use dirname to get rid of "season 3" leaving me with
Code:
/home/xbmc/sab_downloads/video/tv/grey's anatomy

At this point what I want to save to a variable is "grey's anatomy"

The main goal is to take the path and save the show's title to a variable
# 5  
Old 10-06-2008
In that case you can use cfajohnson solution
# 6  
Old 10-06-2008
Quote:
Originally Posted by cfajohnson

The shell has parameter expansion that can do what you want; there is no need for an external command:

Code:
path="/home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3"
showseason=${path#/home/xbmc/sab_downloads/video/tv/}
show=${showseason%/*}

cfa! this works brilliantly, can you give me a quick rundown of what you did here, I am interested in how this works
# 7  
Old 10-06-2008

Read the "Parameter Expansion" section of your shell's man page (or the POSIX Parameter Expansion page, or chapter 1 of my book, Shell Scripting Recipes, or any of the many tutorials on the web).

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

I save the result in a variable

I have friends that this command worked perfectly, but I would like to save the result in a variable, which I have not achieved var=prueba.txt echo $var | cut -d "." -f 1 prueba I need to do this but does not work me salida=echo $var | cut -d "." -f 1 echo "result:$salida" ... (8 Replies)
Discussion started by: tricampeon81
8 Replies

2. UNIX for Advanced & Expert Users

How to save a data of a file into a variable?

My requirement is to read a column data from a file & save it in a variable for each row & process it. I am using the below code- Leadlines="$TGTFILE/Error.txt">>$log_file while read line do id = ` echo $line | cut -d "," -f1 ` email = ` echo $line | cut -d "," -f2 ` ----------- done My... (2 Replies)
Discussion started by: saga20
2 Replies

3. Shell Programming and Scripting

cut delimiter and save

i have list of names in a text file and I wanted to delete the commas en every line. for example: inside the text is a list of names in it with commas after each line. I wanted to delete these commas. which command must I execute for this? unix, dale, shawn, aaron, (4 Replies)
Discussion started by: garfish
4 Replies

4. UNIX for Dummies Questions & Answers

Solved: how to save an output to a variable

i want to save the output of /scripts/whoowns domain.com to a username like $user = /scripts/whoowns domain.com but I'm not sure how to do that This is inside a bash script how can I get the output of /scripts/whoowns then save that to a variable? thanks! ---------- Post updated at... (0 Replies)
Discussion started by: vanessafan99
0 Replies

5. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. Shell Programming and Scripting

Save output in a variable

Hi, I have a 3rd party tool on Solaris 8. I am running it thorugh my script but I am not able to capture its output into a variable. Like, I tried, but did not work. output=`/usr/bin/myTool` echo $output Also, I tried saving in a file but when I run, output is always shown on the... (19 Replies)
Discussion started by: angshuman_ag
19 Replies

7. Shell Programming and Scripting

Cut portion of a field in shell scripts

Hi, I am a file with the following layout. field1|field2|field3|field4|field5|field6|field7 field1|field2|field3|field4|field5|field6|field7 field1|field2|field3|field4|field5|field6|field7 I need to write a file with the below layout field1|field2|fieldx|field6 where fieldx =... (5 Replies)
Discussion started by: shivacbz
5 Replies

8. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

9. UNIX for Dummies Questions & Answers

Save contents of a Variable

I want to save the contents of a variable to a file. How can that be achieved? I have tried with: echo $varname > textfile.txt but for some reason it does not print anything. (1 Reply)
Discussion started by: carl_vieyra
1 Replies
Login or Register to Ask a Question