LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-27-2019, 12:21 AM   #1
xray55
Member
 
Registered: Aug 2019
Posts: 46

Rep: Reputation: Disabled
Simple Database Query


MySql57
table name: streams
field name: stream_source



Hey guys,

Im trying to edit about 235 weblinks inside a small database.


I want to remove the highlighted characters

http://domain:9595/live/USERNAME/PASSWORD/425.m3u8


and end up with streams that look like

http://domain:9595/USERNAME/PASSWORD/425

removing find/replace .m3u is easy enough but I need to also remove the word live and one of the slashes besides it so I dont end up with double slashes //


This is what I attempted

Code:
update streams set stream_source = replace(stream_source, '/live/', '/');
update streams set stream_source = replace(stream_source, '.m3u8', '');
exit;

the .m3u8 is replaced with blank so that works but slash / is not replacing /live/


my understanding is that u cant search and replace slashes /

what can I do to get around this?

Last edited by xray55; 10-27-2019 at 02:45 PM.
 
Old 10-27-2019, 01:43 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Completely untested, but try
Code:
update streams set stream_source = replace(stream_source, 'live/', '');
Also, try your code and my try using double quotes.
And try escaping the slash.
[Confession: I did not know about the replace() function until now...very cool -- I don't have time to look it up right now...getting late here. ]

Although I will point out that a double slash is treated the same as a single slash, usually.

Code:
[root@localhost:~]# ls -l lqtest/testdir/
total 0
-rw-r--r-- 1 root root 0 Oct 26 23:42 file1
-rw-r--r-- 1 root root 0 Oct 26 23:42 file2
[root@localhost:~]# ls -l lqtest//testdir/
total 0
-rw-r--r-- 1 root root 0 Oct 26 23:42 file1
-rw-r--r-- 1 root root 0 Oct 26 23:42 file2

Last edited by scasey; 10-27-2019 at 01:49 AM.
 
Old 10-27-2019, 03:10 AM   #3
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
hey scasey

thank u..

I tried it but it ignore it with no change

Code:
mysql> update streams set stream_source = replace(stream_source, 'live/', '');
Query OK, 0 rows affected (0.02 sec)
Rows matched: 1251  Changed: 0  Warnings: 0

Last edited by xray55; 10-27-2019 at 03:13 AM.
 
Old 10-27-2019, 03:19 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You could move the problem to bash level. I.e. write a shell script that uses mysql with the -e option to first obtain the URL, then processes it with the usual suspects sed and/or awk, finally update the record again with mysql -e.
 
Old 10-27-2019, 04:06 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
The right most 3 bytes seems easy enough. replace(field_name) with left(FIELD_NAME, len(FIELD_NAME),3) ..

Next, to remove the word live that is obtained as the second element when the contents are SPLIT with forward slash as the delimiter. I am looking at using SPLIT with SUBSTRING_INDEX. I just googled and got this. https://coderwall.com/p/zzgo-w/split...ngs-with-mysql Does it help? Maybe SUBSTRING_INDEX - I didn't know about - can be used for the first part (last 3 bytes) also.

Alternatively, if it a small table (less rows) AND you can identify a unique field AND it's a one time job,
1. Dump out to a text file,
2. Change,
3. Update back.

OK

Last edited by AnanthaP; 10-27-2019 at 05:44 AM.
 
Old 10-27-2019, 04:19 AM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by xray55 View Post
hey scasey

thank u..

I tried it but it ignore it with no change

Code:
mysql> update streams set stream_source = replace(stream_source, 'live/', '');
Query OK, 0 rows affected (0.02 sec)
Rows matched: 1251  Changed: 0  Warnings: 0
What about using double quotes?
Code:
mysql> update streams set stream_source = replace(stream_source, "live/", '');
Or escaping the slash?
Code:
mysql> update streams set stream_source = replace(stream_source, 'live\/', '');
 
Old 10-27-2019, 01:56 PM   #7
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
You could move the problem to bash level. I.e. write a shell script that uses mysql with the -e option to first obtain the URL, then processes it with the usual suspects sed and/or awk, finally update the record again with mysql -e.

thanks berndbausch, I can barely write simple scripts. I dont think I can handle that one lol.



Quote:
Originally Posted by AnanthaP View Post
The right most 3 bytes seems easy enough. replace(field_name) with left(FIELD_NAME, len(FIELD_NAME),3) ..

Next, to remove the word live that is obtained as the second element when the contents are SPLIT with forward slash as the delimiter. I am looking at using SPLIT with SUBSTRING_INDEX. I just googled and got this. https://coderwall.com/p/zzgo-w/split...ngs-with-mysql Does it help? Maybe SUBSTRING_INDEX - I didn't know about - can be used for the first part (last 3 bytes) also.

Alternatively, if it a small table (less rows) AND you can identify a unique field AND it's a one time job,
1. Dump out to a text file,
2. Change,
3. Update back.

OK

Thanks AnanthaP, I knew that was an option. I was afraid something would get changed or screwed up when I resave.



Quote:
Originally Posted by scasey View Post
What about using double quotes?
Code:
mysql> update streams set stream_source = replace(stream_source, "live/", '');
Or escaping the slash?
Code:
mysql> update streams set stream_source = replace(stream_source, 'live\/', '');

Thanks. I will try tonight
 
Old 10-27-2019, 02:23 PM   #8
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
scasey, unfortunately, they didnt work, both caused sql error in your SQL syntax.
 
Old 10-27-2019, 02:27 PM   #9
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
If I just ignore slashes all together and just replace the word live

Code:
update streams set stream_source = replace(stream_source, 'live', '');
It will end up with a link

http://domain:9595//USERNAME/PASSWORD/425

which isnt the end of the world, double slashed links // will still resolve. At least in a browser.

I just wanted a clean replace and to finally learn how to replace special characters with sql

Last edited by xray55; 10-27-2019 at 02:28 PM.
 
Old 10-27-2019, 03:08 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by xray55 View Post
scasey, unfortunately, they didnt work, both caused sql error in your SQL syntax.
What did the error say?
 
Old 10-27-2019, 05:48 PM   #11
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
What did the error say?





Code:
update streams set stream_source = replace(stream_source, 'live/', '');
mysql> update streams set stream_source = replace(stream_source, 'live/', '');
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1272 Changed: 0 Warnings: 0



Code:
mysql> update streams set stream_source = replace(stream_source, "live/", '');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql > update streams set stream_source = replace(stream_source, "live/", '')' at lin e 1


Should I use single quotes around the double quoutes or vice versa?

"live/"
'"live/"'
"'live/'"
 
Old 10-27-2019, 07:27 PM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by xray55 View Post
Code:
update streams set stream_source = replace(stream_source, 'live/', '');
mysql> update streams set stream_source = replace(stream_source, 'live/', '');
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1272 Changed: 0 Warnings: 0
That query appears to have worked, except for the "0 rows afftected" part.
Try just
Code:
SELECT replace(stream_source, 'live/', '') FROM stream where stream_source like '%live%';
to test. It won't change anything in the database, just dump the output to the screen. A slash is not a special character, as far as I know. It shouldn't matter. Testing the replace() syntax without trying to change the database is where to start. IMO.


Quote:
Originally Posted by xray55 View Post
Code:
mysql> update streams set stream_source = replace(stream_source, "live/", '');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> update streams set stream_source = replace(stream_source, "live/", '')' at line 1
That attempt appears to have included the mysql prompt (highlighted).

Did you try escaping the slash:
Code:
update streams set stream_source = replace(stream_source, 'live\/', '')
Quote:
Originally Posted by xray55 View Post
Should I use single quotes around the double quoutes or vice versa?

"live/"
'"live/"'
"'live/'"
Either double-quotes or escaping the slash should work, but again, test the replace syntax first with
Code:
SELECT replace(...) from streams where ...;
maybe the problem lies elsewhere.
 
Old 10-27-2019, 07:40 PM   #13
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
The replace() function works just fine to remove slashes like this:
Code:
SELECT caption, replace(caption, '/', '') FROM `photos` WHERE caption like '%/%'
Of course, you don't want to remove all the slashes (nor did I in that example...I'd have messed up several records in my database). Once I get that query refined, I'll try your UPDATE SET.

Definitely want to pay attention to the WHERE clause...

Code:
SELECT caption, replace(caption, '/', '') FROM `photos` WHERE caption like '%in/%'
Modeling in/ New York for Barbizon -- 1943 	Modeling in New York for Barbizon -- 1943
So the problem is not the slash...

UPDATE: Syntax using your UPDATE...SET logic worked just fine. That caption had the / removed from the only row that contained an '%in/%. The slash is not the problem with your syntax.

Do you (the user you're logged into mysql with) have UPDATE privileges on the table?

Last edited by scasey; 10-27-2019 at 07:58 PM.
 
Old 10-27-2019, 08:50 PM   #14
xray55
Member
 
Registered: Aug 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
The replace() function works just fine to remove slashes like this:
Code:
SELECT caption, replace(caption, '/', '') FROM `photos` WHERE caption like '%/%'
UPDATE: Syntax using your UPDATE...SET logic worked just fine. That caption had the / removed from the only row that contained an '%in/%. The slash is not the problem with your syntax.

Do you (the user you're logged into mysql with) have UPDATE privileges on the table?

Yes, its root user
 
Old 10-27-2019, 09:21 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by xray55 View Post
Yes, its root user
You have a MySQL user ‘root’?
Although in your OP you said that replacing the ‘. m3u8’ worked...so it must not be permissions...it was just a thought — an “is it plugged in?” question. Sorry.

Did you test what you’re trying with the SELECT…replace(…) syntax? What happened?

Again, the slash made no difference in my tests. There’s nothing special about a slash in that context.
 
  


Reply



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
Simple Slackware vs simple Arch vs simple Frugalware punchy71 Linux - Distributions 2 08-28-2012 02:30 PM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
non Recursive query and Recursive query prashsharma Linux - Server 1 06-27-2007 09:33 AM
Database Programming (database to database transaction) johncsl82 Programming 7 02-02-2007 08:20 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:13 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