I am very new to shell scripting, the command line, sed, awk, etc so bear with me.
I have a script that outputs -
Reseller: iwantmyname http://iwantmyname.com
I want it to read -
Reseller: iwantmyname
Dropping anything starting with http
I figured SED would be a good tool but I only have a basic knowledge of it, and the tutorials I've found online seem advanced and difficult for me.
I know the basic is sed 's/find_this/replace_with_this/' and I figured I'd replaced the found http with // or nothing. But I don't know how to search for something that starts with http and include EVERYTHING after it. I've looked up regex but that seems quite difficult as well.
Replace a white space followed by http and rest of row with nothing:
's/ http.*//'
Related
I must admit to sed seeming at times to be a bit of a black art to me; I found the following statement, which provided some of what I require. I am assuming that sed is my handiest option as it will be in a bash script.
I have a file with lots of stuff, e.g.
LOTS_OF_OTHER_STUFF/STRING1\nOL8.0:2019-10-08-2/STRING2/LOTS_OF_OTHER_STUFF_HERE/STRING1\nOL8-slim:2019-10-08-20/SRING2/LOTS_OF_OTHER_STUFF
sed '/STRING1/!d;s//&\n/;s/.*\n//;:a;/STRING2/bb;$!{n;ba};:b;s//\n&/;P;D'
\nOL8.0:2019-10-08-2/
\nOL8-slim:2019-10-08-20/
What I require is:
8.0 2
8-slim 20
Can anyone help?
There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...
It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the \n and replace all occurences of <tag by \n<tag ... so simple. Why it is not simple to do by terminal?
The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash? Example using that solution: sed 's/<article/\'$'\n\n<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.
So, this quetion is not about "any solution", but about "some simple/elegant solution".
If I understand what you are looking for you could try something like the following:
sed 's/<tag>/\n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
I have an infected website, and I am trying to clean it out using sed. Unfortunately I am unable to escape the question mark sign in the URL and I am really stuck here. I've searched over the web for a possible solution, but unfortunately I didn't found a proper way to do so.
Just an explanation:
The injected code is similar to this one:
< iframe src=http://test.com/index.html?i=23123>< /iframe>
Note that I am not a pro, and there is why I need your help!
so my way to clear the code is :
sed -i '/< iframe src=http:\/\/test.com\/index.html\?i=23123>/,/< \/iframe>/d' index.html
Unfortunately that didn't help as well as all others.
All help will be gratefully appreciated.
echo "< iframe src=http://test.com/index.html?i=23123>< /iframe>" \
| sed 's#< iframe src=http://test.com/index.html?i=23123>< /iframe>##'
Produces no output, which to me means this is successfully deleting your problem string.
Note that most seds will accept an alternate regex-replacement character, here I am using # because there are no #s in the search target. On some seds, you have to tell it 'hey I'm using an alternate, and escape the char, like s\#.....##.
I don't see why your attempt to quote the ? is failing. Did you try [?] and (worst case) [\?]. Are there 2nd level evaluations happening by the shell that you're not mentioning here? Does my simple example also fail?
As others will certainly tell you, your approach is strictly a bandaid, you need to figure out what the security hole is in your system and fix it. Your pages will get corrupted again. :-(
IHTH
I've just stumbled upon some cryptic sed expression in a legacy script. Could you give me some hints how to start decoding it?
Best thing would be some automatic tool translating sed incantations to English, but for a close runner up, I'd be very grateful for some nice index of (all) sed commands. Otherwise, I'm certainly highly interested in any help at all on how to quickly attack the problem (other than having to read the manual cover to cover...).
(Side note: as you may have guessed, I don't want to just paste the expression here, as I'd like to be able to do it easier and faster next time I stumble on some similar line noise...)
I'd be very grateful for help!
Edit: regexps themselves aren't problem, by the way, I'm good enough at them.
i don't think there is automatic tool that can 'transalte' sed commands to english. however you may want to check http://aurelio.net/sedsed/ . it will help you to understand one sed script, what it does, and how.
anyway, if you list some examples would be good.
This might work for you.
Unix in a Nutshell by Robbins has a very nice chapter on sed. Clear and concise descriptions of the commands.
Your best bet would be to learn the sed language in-depth. Unforunately, the sed documentation is more like a reference. Here's a nice step by step guide that doesn't take too long to read.
I found "Sed One-Liners Explained" to be very informative as well as fun.
I would like to generate Perl Expect code automatically, does something like autoexpect exist for Perl's Expect??
This is not a good answer, but will have to do until a good answer comes along.
I ran the TCL autoexpect and it created a script file, I then wrote couple lines of perl code that parses the lines with "send" and "expect" tags and then uses the perl expect module to run them along with some other actions.
This hybrid approach gets me by, but I am still hoping for a better answer to come.