Add more hash to existing hash script in multiple files - bash

I've php script files that store password to MySQL using md5 hash
Here's some of existing query code in that php files
some query part....,MD5('$pass'),.....some query part
Some script has lowercase md5
some query part....,md5('$pass'),.....some query part
My goal is to add another hash to existing code, like, sh1
So the expected query script should be like this
some query part....,sh1(MD5('$pass')),.....some query part
at the end, the current md5 isn't matter if it's in lowercase or uppercase
I've tried to use sed to replace that files but no luck
sed -i 's/md5\(.*\)/sha1\(md5\(.*\)\)/gI' *.php
Is there anyone can help me?
Thanks

You may use
sed -i 's/\bmd5([^()]*)/sha1(&)/gI' *.php
The POSIX BRE expression matches:
\b - a word boundary
md5( - an md5( substring
[^()]* - 0 or more chars other than ( and )
) - a ) char.
The sha1(&) replacement pattern replaces the match woth sha1(, then the match value, and then ).
See the online demo:
s='some query part....,MD5('"'"'$pass'"'"'),.....some query part
some query part....,md5('"'"'$pass'"'"'),.....some query part'
sed 's/\bmd5([^()]*)/sha1(&)/gI' <<< "$s"
Output:
some query part....,sha1(MD5('$pass')),.....some query part
some query part....,sha1(md5('$pass')),.....some query part

Related

edit attribute value in bash shell unix script

Guys i have an xml where they have requestIDattrbute and i want to change its value - it can be in lof of forms like this:
<ns0:requestID>12345</ns0:requestID>
<requestID>12345</requestID>
<requestID>12345667</requestID><anyOtherAttribute>131241</anyOtherAttribute>
any suggestion how to make it through "sed" ? - Thanks
any suggestion how to make it through "sed" ?
This simple substitution command handles the shown cases:
sed 's,\<requestID>[^<]*,requestID>CHANGE,'
can you tell me what does it means ?
s,regexp,replacement, - Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.
\< - match the empty string at the beginning of a word
[^<] - matches any character but <
* - The preceding item will be matched zero or more times.
CHANGE - value to which you want to change

Extract a substring (value of an HTML node tag) in a bash/zsh script

I'm trying to extract a tag value of an HTML node that I already have in a variable.
I'm currently using Zsh but I'm trying to make it work in Bash as well.
The current variable has the value:
<span class="alter" fill="#ffedf0" data-count="0" data-more="none"/>
and I would like to get the value of data-count (in this case 0, but could be any length integer).
I have tried using cut, sed and the variables expansion as explained in this question but I haven't managed to adapt the regexs, or maybe it has to be done differently for Zsh.
There is no reason why sed would not work in this situation. For your specific case, I would do something like this:
sed 's/.*data-count="\([0-9]*\)".*/\1/g' file_name.txt
Basically, it just states that sed is looking for the a pattern that contains data-count=, then saves everything within the paranthesis \(...\) into \1, which is subsequently printed in place of the match (full line due to the .*)
Could you please try following.
awk 'match($0,/data-count=[^ ]*/){print substr($0,RSTART+12,RLENGTH-13)}' Input_file
Explanation: Using match function of awk to match regex data-count=[^ ]* means match everything from data-count till a space comes, if this regex is TRUE(a match is found) then out of the box variables RSTART and RLENGTH will be set. Later I am printing current line's sub-string as per these variables values to get only value of data-count.
With sed could you please try following.
sed 's/.*data-count=\"\([^"]*\).*/\1/' Input_file
Explanation: Using sed's capability of group referencing and saving regex value in first group after data-count=\" which is its length, then since using s(substitution) with sed so mentioning 1 will replace all with \1(which is matched regex value in temporary memory, group referencing).
As was said before, to be on the safe side and handle any syntactically valid HTML tag, a parser would be strongly advised. But if you know in advance, what the general format of your HTML element will look like, the following hack might come handy:
Assume that your variable is called "html"
html='<span class="alter" fill="#ffedf0" data-count="0" data-more="none"/>'
First adapt it a bit:
htmlx="tag ${html%??}"
This will add the string tag in front and remove the final />
Now make an associative array:
declare -A fields
fields=( ${=$(tr = ' ' <<<$htmlx)} )
The tr turns the equal sign into a space and the ${= handles word splitting. You can now access the values of your attributes by, say,
echo $fields[data-count]
Note that this still has the surrounding double quotes. Yuo can easily remove them by
echo ${${fields[data-count]%?}#?}
Of course, once you do this hack, you have access to all attributes in the same way.

How to get date and string separately in a given file name using shell script

Hi I am trying to get the date and string separately from the given file name but not getting exact idea how to do it.
This is the file name "95FILRDF01PUBLI20170823XEURC0V41000.XML"
I want to extract date "20170823" and string "XEUR" from this file name.
I was going through lots of posts in Stackexchange/Stackoverflow, but didn't understand the regular expression they are using.
https://unix.stackexchange.com/questions/182563/how-to-extract-a-part-of-file-name-in-unix-linux-shell-script
Extract part of filename
To extract date and name:
$ name="95FILRDF01PUBLI20170823XEURC0V41000.XML"
$ echo "$name" | sed -E 's/.*([[:digit:]]{8})([[:alpha:]]{4}).*/date=\1 name=\2/'
date=20170823 name=XEUR
The key part of the regex is ([[:digit:]]{8})([[:alpha:]]{4}). The first part of that, ([[:digit:]]{8}) matches 8 digits and saves them as group 1. The second part of that, ([[:alpha:]]{4}) matches four letters that follow the date and saves them as group 2.
The key part is surrounded by .* before and .* after which matches whatever is left over.
The replacement text is date=\1 name=\2 which formats the output.

How to use a regular expression to get the following pattern?

Hello I have the following text:
some text,+
this field is another parameter
this is the final of the field
t10681374flp
t10681375flp
I would like to match the following two lines:
t10681374flp
t10681375flp
the rule is that these words begin with 't' and end with 'p',
I tried:
grep -e t*p testing
however I got:
this field is another parameter
t10681374flp
t10681375flp
So I really would like to appreciate support to overcome this task,
Using grep, to avoid matching strange lines and the perfect match, the code below
grep "^t[0-9]*flp$" testing
This matches the below lines,
t10681374flp
t10681375flp
This doesn't match the lines as below,
this field is another parameter
these dont grep
Hope you get resolved..
Following should do the work:
grep ^t.*p$ testing
^ indicates begining of the line, .* indicates any character
and $ indicates end of line.

Using placeholders/variables in a sed command

I want to store a specific part of a matched result as a variable to be used for replacement later. I would like to keep this in a one liner instead of finding the variable I need before hand.
when configuring apache, and use mod_rewrite, you can specificy specific parts of patterns to be used as variables,like this:
RewriteRule ^www.example.com/page/(.*)$ http://www.example.com/page.php?page=$1 [R=301,L]
the part of the pattern match that's contained inside the parenthesis is stored as $1 for use later. So if the url was www.example.com/page/home, it would be replaced with www.example.com/page.php?page=home. So the "home" part of the match was saved in $1 because it was the part of the pattern inside the parenthesis.
I want something like this functionality with a sed command, I need to automatically replace many strings in a SQL dump file, to add drop table if exist commands before each create table, but I need to know the table name to do this, so if the dump file contains something like:
...
CREATE TABLE `orders`
...
I need to run something like:
cat dump.sql | sed "s/CREATE TABLE `(.*)`/DROP TABLE IF EXISTS $1\N CREATE TABLE `$1`/g"
to get the result of:
...
DROP TABLE IF EXISTS `orders`
CREATE TABLE `orders`
...
I'm using the mod_rewrite syntax in the sed command as a logical example of what I'm trying to do.
Any suggestions?
sed '/CREATE TABLE \([^ ]*\)/ s//DROP TABLE IF EXISTS \1; &/'
Find a CREATE TABLE statement and capture the table name. Replace it with 'DROP TABLE IF EXISTS' and the table name, plus a semi-colon to terminate the statement, and a copy of what was matched to preserve the CREATE TABLE statement.
This is classic sed notation. Since you're using bash, there's a chance you're using GNU sed and will need to add --posix to use that notation, or you'll need to fettle the script to use GNU's non-standard sed regexes. I've also not attempted to insert a newline into the output. You can do that with GNU sed if it is important enough to you.
The key points are the parentheses (classically needing to be escaped with a backslash) are the capture mechanism, and backslash-number is the replacement mechanism.
sed -r "s/CREATE TABLE (\`.*\`)/DROP TABLE IF EXISTS \1\n &/g" dump.sql
test:
kent$ cat t.txt
CREATE TABLE `orders`
...
CREATE TABLE `foo`
...
...
CREATE TABLE `bar`
...
kent$ sed -r "s/CREATE TABLE (\`.*\`)/DROP TABLE IF EXISTS \1\n &/g" t.txt
DROP TABLE IF EXISTS `orders`
CREATE TABLE `orders`
...
DROP TABLE IF EXISTS `foo`
CREATE TABLE `foo`
...
...
DROP TABLE IF EXISTS `bar`
CREATE TABLE `bar`
...
This is called a "back reference". And sed will start numbering the things between the parenthesis. \(...\)
Note the use of backslash as an escape character above.
Ref: https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
Here is an answer to the OP's question via title of this thread:
As #bhoom-suktitipat states: This is called a "back reference"...
SUMMARY:
Placeholder variables, or back reference lookup is achieved, on the replace side, by using backslash followed by a digit, starting at 1, like so: \1
BACKGROUND: I'm here because I've been taking a bunch of code and revamping it. In the process I want to pre-eslint it to find all the double quotes around JSON keys and strip them. ESLint's Quote Props Rule can be used, with --fix, to do the exact opposite of what I need todo.
MISSION OUTLINE:
Turn
{ "foo": "bar" }
into { foo: "bar" }, a.k.a. using-placeholders-variables-in-a-sed-command
Find the equivalent of the "foo" (key) in the second matching group: ("{1})(\w+)(":{1}).
Use that matching group as a placeholder to render foo:, instead of "foo":
Write changes to file.
Write to different file:
sed -r 's/("{1})(\w+)(":{1})/\2:/g' in.js > out.js
Write to same file:
sed -ri 's/("{1})(\w+)(":{1})/\2:/g' in-and-out.js
SED flags used:
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)

Resources