Predator
Predator

Reputation: 126

BASH - Manipulating a URL

I'm busy with a script in bash that's job is to move content from a sub directory to the main directory of an FTP root. If the files should contain a database then the script must update all the SQL entries in the database.

The MySQL queries are not the issue but getting the values for search and replace is evading me.

For example:

I have a site in http:// example.com/mysub

I need to move it to http:// example.com and change the database

I need to remove the http:// from http:// example.com/mysub so I can run a search and replace the values with example.com

Any ideas?

Upvotes: 2

Views: 240

Answers (2)

Rany Albeg Wein
Rany Albeg Wein

Reputation: 3474

You can enable extglob (shot -s extglob) and run the following Parameter Expansion*:

var='https://example.com'
printf '%s\n' "${var#http?(s)://}"

Which will remove the first occurrence of http:// or https:// from the left side of var.


Parameter Expansion* : Execute man bash | less +/Parameter\ Expansion or read further at Bash FAQ 37. Or both!

Upvotes: 0

karakfa
karakfa

Reputation: 67467

sed -r 's_\bhttps?://__'

also handles https, example

$ echo "http://example.com" | sed -r 's_\bhttps?://__'
example.com

$ echo "https://example.com" | sed -r 's_\bhttps?://__'
example.com

Upvotes: 1

Related Questions