Replacing "/\ characters with sed, from within php - php

I generate password from php (thru web page) using shell_exec / dd/ /dev/urandom.
I want to eliminate
"/\
characters from my generated password.
If I do a replacement in my sed line with
's/["/\]/!/g'
the script fails to execute. However I tested this from command line like
echo "this /is \"a \test" | sed -e 's/["/\]/!/g'
Then I get the right result : this !is !a !test
If I eliminate this replacement section from my sed line, php script does execute properly. What seems to be the issue? I thought characters within brackets [] is safe and does not need escaping.. is that the issue?

In PHP, if you use the command in a double quoted string literal, you need to escape the " with a single backslash, \", and the backslash inside the bracket expression must also be escaped.
In the end, this must look like
sed 's/[\"/\\]/!/g'

Related

Replace string with special characters

I'm trying to replace the following string in a wordpress sql file:
http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png
to
https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png
I tried the following command which obviously didn't work
sed -i "s#'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png'#'https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png'#g" database.sql
Someone please help to understand where I missed. Thank you very much.
You can't seriously apply a sed to a .db file because... well, it's database file not text (most likely sqlite by the way).
Instead, you should perform the string replacement with an (UPDATE) SQL query from the SQLite console (or whatever SQL client you have). Check this link for the replace method in SQLite for example.
Your first mistake is enclosing your script in double quotes instead of single, thereby inviting the shell to parse its contents before sed gets to see it and thus eating up one layer of backslashes.
If you have to deal with single quotes (which you shouldn't given your posted sample input but anyway...) never do this:
sed "s/foo'bar/stuff/"
do this instead:
sed 's/foo'\''bar/stuff/'
so the shell isn't interpreting every part of your script.
Beyond that - sed doesn't understand literal strings (see Is it possible to escape regex metacharacters reliably with sed), so instead just use a tool that does, e.g. awk:
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]="" }
s=index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+length(old)) }
1' \
'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png' \
'https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png' \
file
https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png

Find position of console escape code in string

tl;dr How do I write strpos($haystack, '^[[H^[[2J') in PHP?
A linux command line app delivers a screen-full of data followed by a console escape code to return the cursor to the "home" position in a regular loop. To access the data I piped the output to my own script like so:
$ ./otherapp | php myscript.php
It's a continuous stream of data so I use this example for non-blocking stream consumption.
Now to decode the output I need to grab a full frame/screen full of data. The escape codes are shown in nano as ^[[H and ^[[2J. The easiest way seems to be detecting these escape codes and using the output between them.
How are the escape codes represented in a PHP string? Can I use strpos (or mbstring equivalent) to detect their position?
It seems you're on a Unix environment. To achieve what your escape codes do, you can try with echo -en "\033[H" and echo -en "\033[2J", so I guess you should try something like strpos('\033[H\033[2J', $haystack).
Maybe you'll need to add an extra backslash ('\\\033[H\\\033{2J').
Since \033 means "the byte with octal value 33", which is ESC, this should work:
strpos("\e[H\e[2J", $haystack)
You need to enclose it in double-quotes (") if you want PHP to interpret escape sequences for special characters.

How to insert a string with double quotes in the first line of my csv file?

I am looking for a way to insert this:
"random1","random2"
in the first line of my csv file using sed.
Tried hard and only got this:
sed -i -e '1irandom1 random2\' filename
Output is just random1 and random2 without any double quotes - clearly.
Any good idea?
Have you tried
sed -i -e '1i"random","random2"\' filename
If you want the quotes (and the comma), you need to explicitly specify what is written, verbatim.
If you want double quotes, type double quotes:
sed -i~ -e '1s/^/"random1","random2"/' filename
i inserts a line, so I used substitution s, replacing ^, i.e. the beginning of the line, by the given string.
Maybe something like:
sed 's/^.*$/"random1","random2"/g' filename would work.

Replacing the value of input tag across multiple files

I have many fields in my smarty template files like this.
{$email} (this can be anything like -- {$description}, {$variable_name}
I want to replace it with {$email|escape:htmlall} or {$variable_name}
how can i do that in linux? I'm having difficulty writing regex in sed.
Try:
sed 's/{\(\$[^}\|]*\)}/{\1|escape:htmlall}/g'
As a test, use:
echo 'blabla {$email} blieblie {$name} bloebloe ${alreadydone|escape:htmlall}'|sed 's/{\(\$[^}\|]*\)}/{\1|escape:htmlall}/gi'
Things that this regexp does:
It replaces any string like {$} by {$|escape:htmlall}
It also makes sure that two consecutive fields (as in the example) are handled separately. This is done by demanding that in the no '}' may occur (this is done by the [^}\|]* part, which means: a series of zero or more characters which all may be anything but '}' and '|' (see below).
It ignores any tags that already were escaped (by ignoring any tag having a | within the curly braces).
Make a backup before and try this:
grep -rl "{$email}" yourdirectory | xargs sed -i 's/\{\$email\}/{$email|escape:htmlall}/g'
perl -ape 's/({\$\w+)(?!\|escape:htmlall)}/$1|escape:htmlall}/g' file
This is using the negative lookahead feature.
This will ignore variables that already have escape:htmlall.

Executing multiline sed in php: escaping issues

I am trying to run sed to do a multiline search and replace with the following string
$test = "sed -n '1h;1!H;${;g;s/iname=\"".$name.".*item>/".trim(xml)."/g;p;}' ".$file;
exec($test,$cmdresult);
sed is choice since the string to be searched is over 10 mb.
During execution compiler issues a warning
PHP Parse error: syntax error, unexpected ';'
How do I go about solving this?
You need to escape the $ in ${}.
$test = "sed -n '1h;1!H;\${;g;s/iname=\"".$name.".*item>/".trim(xml)."/g;p;}' ".$file;
exec($test,$cmdresult);
In order to let humans read your code, though, you should really split the string up. Create it by concatenating other strings, sprintf or HEREDOC.
Probably the $ sign inside the $test variable makes PHP think that there is another variable that should be expanded.
Try escaping the $ character (\$), and have a look at the relevant PHP strings doc.

Categories