LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2013, 05:36 AM   #1
tauro_kpo
Member
 
Registered: Feb 2007
Distribution: Debian
Posts: 113

Rep: Reputation: 16
Question Remove / from a string in a bash script


Hello people,

I know very little about bash scripting, but reading a couple of online manuals and forums I managed to create a script that gets the title of a youtube video and creates an empty file with the same name:

Code:
#!/bin/bash
name=$(youtube-dl --get-title $1)
touch "$name"
exit 0
It works flawlessly but when there's a video whose title contains slashes / the file is not created. For example http://www.youtube.com/watch?v=nBHo6O1vVlc

How can I remove all the slashes from a title and replace them with a vertical bar | ?

Thank you in advance

Last edited by tauro_kpo; 02-01-2013 at 08:33 AM.
 
Old 02-01-2013, 05:49 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by tauro_kpo View Post
How can I remove all the slashes from a title and replace them with a vertical bar | ?
If it's a variable (e.g. your $name variable), you can do it with parameter substitution:

Code:
name=${name//\//|}
This is really "${name//REPLACE-THIS/WITH-THIS}".

See the advanced bash scripting guide.
 
1 members found this post helpful.
Old 02-01-2013, 07:28 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Note - the "/" is a special character used to delimit directories from files. Normally the "|" is also a special character - it is the pipe symbol used to connect the standard output of one command (the one on the left) with the standard input of the next (the one on the right).

It IS possible to put a "|" into files, but it tends to cause a lot of problems with shell scripts that process lists of files.

The more frequent substition is either a _ (used for spaces) and "." (used for anything else).
 
1 members found this post helpful.
Old 02-01-2013, 08:32 AM   #4
tauro_kpo
Member
 
Registered: Feb 2007
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 16
Thumbs up

That worked fantastic John, I'll save that link.

jpollard I'll keep your advice in mind.

Thank you both very much.
 
  


Reply

Tags
bash, script



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to remove string from all text files, recursively iacchi Programming 9 04-27-2012 05:22 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 11:40 AM
help with bash script: remove * to the last . in string drkstr Linux - Software 3 04-25-2006 04:54 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration