LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2014, 10:59 AM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Rep: Reputation: 3
how would i text manipulate this?


i have this junk code injected into a mysql database for a word press site.

what is the best way to strip that out?

Code:
<p><script language=\"javascript\">\ndocument.write( unescape( \'%3C%21%44%4F%\'));\n</script></p>
 
Old 02-08-2014, 05:36 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
what do you want to strip out?
 
Old 02-08-2014, 07:41 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You want to extract that from something or you wish to extract something from that?

You will need to be a lot clearer if you want help.
 
Old 02-08-2014, 08:15 AM   #4
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
sorry i should have been more clearer.

here is what i have:

Code:
I WANT THIS TEXT <p><script language=\"javascript\">\ndocument.write( unescape( \'%3C%21%44%4F%\'));\n</script></p> I WANT THIS TEXT
here is what i want:

Code:
I WANT THIS TEXT I WANT THIS TEXT
i just need to strip that junk code out.
 
Old 02-08-2014, 09:33 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sounds fair enough ... what have you done and where are you stuck?

If not sure of a tool, then sed could easily remove what is shown.
 
1 members found this post helpful.
Old 02-08-2014, 10:12 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by socalheel View Post
i just need to strip that junk code out.
With this InFile ...
Code:
I WANT THIS TEXT <p><script language=\"javascript\">\ndocument.write( unescape( \'%3C%21%44%4F%\'));\n</script></p> I WANT THIS TEXT
... this awk ...
Code:
awk 'BEGIN{FS=" <|> "} {print $1,$NF}' $InFile >$OutFile
... produced this OutFile ...
Code:
I WANT THIS TEXT I WANT THIS TEXT
Daniel B. Martin
 
2 members found this post helpful.
Old 02-08-2014, 10:55 AM   #7
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
i was able to accomplish what i needed ... since it was a sql database dump, i was able to open it up in sql management studio and the find/replace tool in sql management studio actually did the job.

i do not really like that i used a microsoft tool to do a job linux could handle ... i just am not that powerful with advanced text manipulation.

what really stumped me was all the special characters in that junk code (i.e. <> % @ " \), i don't know how to escape those out.

given this example, can sed or awk strip out text that is between a string?

for example, can i tell sed/awk to strip out anything between "I WANT THIS" and "I WANT THIS"?


edit:
i asked my question after danielbmartin posted his.

from what you posted daniel, that looks a lot like what you told me how to efficiently text manipulate a few months back. i guess as soon as i saw all those special characters in my code, i got intimidated and immediately thought i didn't know how to do that.

thanks for being patient and informative.

Last edited by socalheel; 02-08-2014 at 10:57 AM.
 
Old 02-08-2014, 11:06 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
sed 's/ <.*>//' file
 
2 members found this post helpful.
Old 02-08-2014, 11:22 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Code:
sed 's/ <.*>//' file
Extra points for being so concise. Love it!

Daniel B. Martin
 
1 members found this post helpful.
Old 02-08-2014, 02:26 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by socalheel View Post
thanks for being patient and informative.
Pleased to be of service!

This is a variation on my awk solution posted earlier today. The method is the same but this solution uses fewer keystrokes.
Code:
awk -F" <|> " '{print $1,$NF}' $InFile >$OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 02-08-2014, 09:49 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is a slightly more confusing awk
Code:
awk -F" <.*> " '$1=$1' file
 
1 members found this post helpful.
Old 02-09-2014, 06:16 AM   #12
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
If you want to stop users inserting html or javascript tags in your wordpress you can wrap a

strip_tags($insertText)

around the value to be inserted in the mysql but probably wordpress has a config param for that.
 
1 members found this post helpful.
Old 02-09-2014, 11:05 AM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Here is a slightly more confusing awk
Code:
awk -F" <.*> " '$1=$1' file
It works but I don't understand it. Please explain.

Daniel B. Martin
 
Old 02-09-2014, 11:47 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
-F" <.*> " - - Set field separator (FS) to regex value

'$1=$1' - - recalculate the line replacing FS with OFS. Also, it equates to true, hence print the line
 
2 members found this post helpful.
Old 02-10-2014, 12:29 AM   #15
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
-F" <.*> " - - Set field separator (FS) to regex value

'$1=$1' - - recalculate the line replacing FS with OFS. Also, it equates to true, hence print the line
Clever!

Daniel B. Martin
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Manipulate output alancampbell6 Linux - Newbie 6 10-11-2011 01:17 PM
Script to manipulate multiple text files. tuxlux Programming 3 09-30-2011 10:14 AM
[SOLVED] Manipulate text file to allow import to excel dimothy Linux - Software 3 06-14-2010 07:50 AM
General tool to manipulate text configuration files lazac Linux - Software 6 03-28-2010 08:31 PM
Best Way to manipulate a text file.. php ? 8rucech85 Programming 2 01-30-2008 04:35 AM

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

All times are GMT -5. The time now is 04:57 AM.

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