match regex to remove password from file - regex

I have a file which contain following lines
app.mail.host = 10.1.1.1
app.mail.debug = true
app.db.username = spate
app.db.password = 1#4FnL&#7!
I want to use regex to change actual password and replace with XXXXXXX for security purpose.
I am trying following regex but it doesn't working.
sed 's/app\.db\.password="[^"]\+/app\.db\.password="XXXXXXXX"/g' foo.txt

As Hbcdev noted, you aren't matching due to whitespace. As this appears to be "security" code (in which case -- why are you storing that password in plaintext at all?), it's probably better to be whitespace-tolerant than match the input byte-for-byte. Something like:
sed 's/app\.db\.password[ \t]*=.*/app.db.password="xxxxx"/
(untested) is probably going to work a little more robustly. Note that it will strip your password field even if it doesn't begin with a quote.
Still, doing this kind of hackery with a shell script sounds dangerous. What are you trying to accomplish?

You haven't left spaces around the = sign in your sed expression. Once you do that I think it'll work.

Related

regex replace in lines starting with {\s between first space to ;}

i have some corrupt rtf files with lines like this:
{\s39\li0\fi0\ri0\sb0\sa0\ql\vertalt\fs22 Fußzeile Zchn;}
^----------------------------^
i want to replace all [^a-zA-Z0-9_\{}; ]
but only in lines beginning with "{\s" and ending with "};" from the first "space" to "};"
the first "space" and "};" should not be replaced.
You didn't specify language, here is Regex101 example:
({\\s.+?\s)(.*)(})
So, I'm unsure what language/technology you'd like to use here, but if using C# is an option, you can check out this previous question. The answer gets you almost the way there.
For your example:
var text = #"{\s39\li0\fi0\ri0\sb0\sa0\ql\vertalt\fs22 Fußzeile Zchn;}";
var pattern = #"^({\\s\S*\s[a-zA-Z0-9_\{}; ]*)([^a-zA-Z0-9_\{}; ]*)([^}]*})";
var replaced = System.Text.RegularExpressions.Regex.Replace(text, pattern, "$1$3");
This will get you to replace one contiguous blob of bad characters, which addresses your example, but unfortunately, not your question. There is probably a more elegant solution, but I think you'll have to iteratively run that expression until the input and output of Regex.Replace() are equal.
If you can use sed in a terminal, you could do something like this.
sed -i 's/^\({\\s[^ ]*\s\).*\(\;}\)\(}\)\?$/\1\2/' filename
Turned my file containing:
{\s39\li0\fi0\ri0\sb0\sa0\ql\vertalt\fs22 Fußzeile Zchn;}
To:
{\s39\li0\fi0\ri0\sb0\sa0\ql\vertalt\fs22 ;}

regex to convert custom property file to json key value pair representation

I have a partial solution to convert this
USERNAME=CONSTANT[myUserName]
PASSWORD=CONSTANT[mypwd]
to
"USERNAME":"myUserName",
"PASSWORD":"mypwd"
I see a similar solution here
properties file to json. Basically I am looking for zero or more spaces 1.) anywhere before or after a key 2.) before and after = sign
USERNAME = CONSTANT[myUserName]
PASSWORD = CONSTANT[mypwd]
Find What: (^[^ \t]+)(\s.*=\s*CONSTANT\[)(.*[^\n])(\])
Replace: "$1":"$2",
"USERNAME":"myUserName",
"PASSWORD":"mypwd",
Also I want to make sure I do this for each line and some times it matches multiple lines which is wrong. I hope one can find a solution that works in Eclipse on Windows.
Make sure to use ^ and $ in order to avoid your regex matching multiple lines. Try something like this:
^\s*(\w+)\s*?\=\s*?\w+\[(\w+)\]$
Replace with:
"$1":"$2",
Demo: https://regex101.com/r/mxF8lI/1/

What is the correct sed (with regex) command to replace this piece of text?

Im making a script that has to replace a string of text in a file, this piece of text is part fixed and part random.
I've been trying to fix this for quite a while with some Regex tools on the web, but cant figure it out...
The string that needs to be replaced is:
$mysql_pass = 'password';
Note: 'password' is a random alphanumeric string.
I want to change 'password' (or a random password) to something new, this new password will be generated before and be put in a variable, lets say $pass.
$mysql_pass = '$pass';
AKA
$mysql_pass = 'xOaL4c5ETgt0Izm';
Maybe is should use different command, I dont know, but i rather not download other programs.
Thanks in advance.
With GNU sed:
sed -i 's/$mysql_pass = .*/$mysql_pass = '"'$pass'"';/' file

Need regex to strip away remaing part of a path

I am trying to write a regex which will strip away the rest of the path after a particular folder name.
If Input is:
/Repository/Framework/PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces/IDemoReader.cs
Output should be:
/Repository/Framework/PITA/branches/ChangePack-6a7B6
Some constrains:
ChangePack- will be followed change pack id which is a mix of numbers or alphabets a-z or A-Z only in any order. And there is no limit on length of change pack id.
ChangePack- is a constant. It will always be there.
And the text before the ChangePack can also change. Like it can also be:
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces
My regex-fu is bad. What I have come up with till now is:
^(.*?)\-6a7B6
I need to make this generic.
Any help will be much appreciated.
Below regex can do the trick.
^(.*?ChangePack-[\w]+)
Input:
/Repository/Framework/PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces/IDemoReader.cs
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces
Output:
/Repository/Framework/PITA/branches/ChangePack-6a7B6
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6
Check out the live regex demo here.
^(.*?ChangePack-[a-zA-Z0-9]+)
Try this.Instead of replace grab the match $1 or \1.See demo.
https://regex101.com/r/iY3eK8/17
Will you always have '/Repository/Framework/PITA/branches/' at the beginning? If so, this will do the trick:
/Repository/Framework/PITA/branches/\w+-\w*
Instead of regex you could can use split and join functions. Example python:
path = "/a/b/c/d/e"
folders = path.split("/")
newpath = "/".join(folders[:3]) #trims off everything from the third folder over
print(newpath) #prints "/a/b"
If you really want regex, try something like ^.*\/folder\/ where folder is the name of the directory you want to match.

How to put .com at the end of email addressed by regex?

Example
I received a email-list from my friends but the problem is some people typed an email in full form (xxx#example.com) and some people typed (xxx#xxx without .com). And i want to improve it into the same format. How can i improve it if i want to edit them on vi?
In my emaillist.txt
foo#gmail
bar#hotmail.com
bas#gmail
qux#abc.com
mike#abc
john#email
My try:
i tried to use an easy regex like this to catch the pattern like xxx#xxx
:%s/\(\w*#\w*\)/\0.com/g
or
:%s/\(\w*#\w*[^.com]\)/\0.com/g
But the problem is this regex include xxx#example.com also
And the result become like this after i enter the command above
foo#gmail.com
bar#hotmail.com.com
bas#gmail.com
qux#abc.com.com
mike#abc.com
john#email.com
So, My expectation after substitution is should be like this:
foo#gmail.com
bar#hotmail.com
bas#gmail.com
qux#abc.com
mike#abc.com
john#email.com
How to use regex in this situation?
You can use this command:
%s/^.*\(\.com\)\#<!$/\0\.com/g
The search pattern matches each line not ending with .com (i just copy-pasted the recipy from Vim: Find any line NOT ending in "WORD") and replaces it with itself with .com added.
for gmail.com there is no need of further replace so do replace for only gmail like this
/(.*)(?!\.com)\n/\.com/msi ( i considered as each mail in one new line. )
pls dont -vte mar i tried to explain