Delete the characters of a line between two limiters including themselves - sed

After hacking I need clean some code add to first line on too many files.
<?php $somevar = 'some code....... ?><?php
I need clean between and include <?php $somevar and ?> because latest <?php can to be different such <html and others.
If use this I need two steeps, because not clean delimiters. I need delete delimiters also.
sed -i.bak 's/\(<?php $drnrwsrl\).*\(?>\)/\1\2/' file.php
Result
<?php $drnrwsrl?><?php
Instead of what I want
<?php

Try
sed -i.bak -E 's/(<\?php.*\(?>)(<\?php)/\2/' file.php

Using BRE without backreference:
sed -i.bak 's/<?php \$somevar[^>]*><?php/<?php/' file.php
To apply only to first line:
sed -i.bak '1s/<?php \$somevar[^>]*><?php/<?php/' file.php

Related

Use nested perl command to replace part of line

I have two perl commands and I want to apply one of the commands to replace a capture group in the other command.
Command 1 converts a CamelCase string to snake_case.
It works as expected:
echo 'AbcDefGhi' \
| perl -p -E 's/(?:\b|(?<=([a-z])))([A-Z][a-z]+)/(defined($1) ? "_" : "") . lc($2)/eg'
Result:
abc_def_ghi
Command 2 finds the CamelCase strings that need to be replaced.
It works fine, too (replacing with XXX$1YYY here):
echo ' <div id="AbcDefGhi">' \
| perl -p -E 's# <div id="([^"]*)">#<div id="XXX$1YYY">#s'
Result:
<div id="XXXAbcDefGhiYYY">
But how would I combine both commands now? I tried a lot of stuff, but can't get it to work. My best, probably very bad, attempt was to use the e modifier in command 1 and then printf "$1" and pipe that into command 2, but then I think it accessed the wrong $1 from the outside perl command.
Ideally I want to have it work something like this:
echo ' <div id="AbcDefGhi">' | perl -p -E 's# <div id="([^"]*)">#MAGIC#s'
Result:
<div id="abc_def_ghi">
Solutions with other tools are welcome too, but I would really like to improve to perl knowledge.
So basically one one-liner is to locate the correct string, and the other to change that string. Well, you're already using the /e modifier, so why not just insert the substitution in there?
perl -p -E 's# <div id="([^"]*)">#
qq(<div id=") .
$1 =~ s/(?:\b|(?<=([a-z])))([A-Z][a-z]+)/(defined($1) ? "_" : "") . lc($2)/egr .
qq(">)#sex'
Basically we did this:
perl -pE' s# .... # s/...// # '
It's a bit of a mouthful to read, so I split it up using the /x modifier, but you can just remove that and put it on a single line.
Note that I added the /r modifier, because we don't want to change $1 (we can't, it's read-only), we just want the value after substitution. And that is what /r does, perform the substitution and return the string, but leave original string unchanged.
Luckily you had also already change delimiters on s/// so we don't even need to do that.
This is what the line would look like, for copy/paste purposes:
perl -p -E 's# <div id="([^"]*)">#qq(<div id=") . $1 =~ s/(?:\b|(?<=([a-z])))([A-Z][a-z]+)/(defined($1) ? "_" : "") . lc($2)/egr . qq(">)#se'
I'm sure the better way is to write a simpler command, but this is the quick way to merge your commands.

remove everything between two characters with sed

I'd like to remove any characters between including them also
<img src=\"/wp-content/uploads/9e580e68ed249dec8fc0e668da78d170.jpg\" / hspace=\"5\" vspace=\"0\" align=\"left\">
I was trying
sed -i -e 's/<img src.*align=\\"left\\">//g' file
You do not say what version of sed you are using, or what shell.
With GNU sed and bash, your attempt was almost there. Try:
sed -i 's/<img src[^>]*align=\\"left\\">//g' file
Explanation:
s/<img src[^>]*align=\\"left\\">/ search for <img src_STUFF_align=\"left\">, where _STUFF_ cannot contain any >
// and replace it with nothing
/g and continue
-i and modify the file
I believe this should work with most version of sed (except for the -i).

Extract data using grep/sed from html tag with special class/id

I need to grep info from website and it is stored like:
<div class="name">Mark</div>
<div class="surname">John</div>
<div class="phone">8434</div>
and etc.
Tried to grep it and parse it later with sed:
grep -o '<div class="name">.*</div>' | sed -e 's?<div class="name">?|?g'
but, when I try to replace with sed -e 's?<\/div><div class="phone">?|?g' - no result
and for every class do the same thing. I cannot delete all html tags (sed 's/<[^>]\+>//g'), and need to do it only for div with this classes.
The output format should be like
|Mark|John|8434|
I need to do it with grep/sed
Using awk should do the job:
awk -F"[<>]" '{printf "%s|",$3}' file
Mark|John|8434|
If you need a new line at the end:
awk -F"[<>]" '{printf "%s|",$3} END {print ""}' file
It creates filed separated by < or > then print the third field with | as separator.

Editing a variable inside of a script with sed

Perhaps I'm stetching bash a bit far, but I have this variable containing a list of urls.
#!/bin/bash
/* returns
/path/page/one.php
/path/subseciton/
/path/to/this/section/
/path/to/yet/aother_section/about.php
etc
*/
list_of_urls = $(pull_urls.sh)
Then I have a for loop running, pulling text content from the old server and the new server. On each of those I'm running various diff commands to see what has changed.
for i in $urls
do
echo $i
storage_area=./working/$i/
mkdir -p $storage_area
xidel http://oldserver/$i -e '//div[#id="maincontent"]//p' > $storage_area/old.txt
xidel http://newserver/$i -e '//div[#id="content"]//p' > $storage_area/new.txt
diff $storage_area/old.txt $storage_area/new.txt > $storage_area/diff.diff
wdiff $storage_area/old.txt $storage_area/new.txt > $storage_area/wdiff.wdiff
done
My problem is that I need to remove a trailing slash. Is it advisable to do so in the following way?
// inside the loop, before xidel calls
i=$(echo $i | sed -e 's/\/$//g')
how about in this way:
kent$ i=foo/
kent$ i=${i%/}
kent$ echo $i
foo
if you prefer doing it with sed, you could consider to use another separator other than /, since you want to use slash in your pattern:
kent$ i=/path/to/this/section/
kent$ i=$(sed 's#/$##'<<<"$i")
kent$ echo $i
/path/to/this/section

Replacing "localhost" in text file using sed

I have such file and I would like to replace $GLOBALS['SERVER_MEMCACHED']='localhost'; with $GLOBALS['SERVER_MEMCACHED']='mydomain.com'; using sed.
How to do this ? I don't want to replace the DB_HOST too.
<?php
$GLOBALS['DB_HOST']='localhost';
$GLOBALS['DB_NAME']='database';
$GLOBALS['DB_LOGIN']='login';
$GLOBALS['DB_PASSWORD']='password';
$GLOBALS['PORT_MYSQL']='3306';
$GLOBALS['PORT_MYSQLI']='3306';
$GLOBALS['SERVER_MEMCACHED']='localhost';
$GLOBALS['PORT_MEMCACHED']='11211';
$GLOBALS['CACHE_TIME']=600;
?>
sed -i '/SERVER_MEMCACHED/s/localhost/mydomain.com/' input