I am trying to parse a file test.txt and append "https://" to the following string within the file:
"1234.def.efj.abcd-1.nopqrs.com"
Note: the string has white space and the quotes at the beginning and end. Also def, efj, and nopqrs.com are static and won't change.
I am trying to accomplish this via sed. So far I have:
sed -e 's#^\s*[\"][0-9]+[\.]def[\.]efj[\.][a-z]+[\-][a-z]+[\-][0-9][\.]nopqrs[\.]com$#\s+[\"]https[\:][\/][\/][0-9]+[\.]def[\.]efj[\.][a-z]+[\-][a-z]+[\-][0-9][\.]nopqrs[\.]com#g' test.txt
When I run that command I just get the file output without the change.
I have gone through other sed posts and can't seem to figure out what is wrong with my expression. Thanks in advance for your help!
Something like this should work :
sed -i 's#"\([^"]*\.nopqrs\.com\)"#"https://\1"#g' file.txt
Explanation :
sed -i : inplace editing. sed will update file.txt accordingly. Note that the behaviour and syntax is implementation specific.
"\([^"]*\.nopqrs\.com\)" :
[^"]*\.nopqrs\.com: we look for any number of characters that are not quotes, followed by .nopqrs.com
\( ... \) : syntax for sed to create a capture group with the content matched by the expression between the parenthesis
\1 : we display the content of the first capture group
Related
The name in the title says it all. However, I'm absolutely the worst with the sed command. So I'm trying to edit the following file:
/var/www/html/phpMyAdmin/config.inc.php
I want to edit the line that says
$cfg['Servers'][$i]['AllowRoot'] = false;
into the following
$cfg['Servers'][$i]['AllowRoot'] = true;
It has so many special characters and whatnot and I have no prior knowledge of how sed works. So here's some commands I've tried to specifically edit that one line.
sed -i "/*.AllowRoot.*/\$cfg['Servers'][\$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
sed -i "/*.AllowRoot.*/$cfg['Servers'][$i]['AllowRoot'] = true;/" /var/www/html/phpMyAdmin/config.inc.php
# this one finds the line successfully and prints it so I know it's got the right string:
sed -n '/AllowRoot/p' /var/www/html/phpMyAdmin/config.inc.php
sed -i "s/'AllowRoot|false'/'AllowRoot|true'/" /var/www/html/phpMyAdmin/config.inc.php
I have absolutely no idea what I'm doing and I'm not learning a whole lot besides the feeling that the last command splits up 'AllowRoot|false' makes sure that both must be present in the sentence to come back as a result. So to my logic, I thought changing the word false into true would make that happen, but nothing. The other commands return... bizarre results at best, one even emptying the file. Or that's one of the commands I had not written down here, I've lost track after 50 attempts. What is the solution here?
The [ and ] need to be escaped to match literal brackets, instead of inadvertently starting a bracket expression. This should work:
$ sed -i "/\$cfg\['Servers'\]\[\$i\]\['AllowRoot'\]/s/false/true/" /var/www/html/phpMyAdmin/config.inc.php
There is not many things to escape in sed. Main problem in your line is / which you have chosen as delimiter (most common, but not required). I suggest you use # and the following will work:
sed -i "s#$cfg['Servers'][$i]['AllowRoot'] = false;<br />#$cfg['Servers'][$i]['AllowRoot'] = true;<br />#g" input.txt
however you need to think about bash interpreter as well. $i and $cfg will be interpreted as variables. My suggestion is that when you want to match a string like this to put the sed expression in a text file like this:
cat allow_root_true.sed
s#['Servers'][]['AllowRoot'] = false;<br />#['Servers'][]['AllowRoot'] = true;<br />#g
and run the command using sed -f like this:
sed -i -f allow_root_true.sed input.txt
Warning -i will change the input file
sed can't do literal string matching which is why you need to escape so many characters (see Is it possible to escape regex metacharacters reliably with sed), but awk can:
$ awk -v str="\$cfg['Servers'][\$i]['AllowRoot']" 'index($0,str){sub(/false/,"true")} 1' file
//some text here
$cfg['Servers'][$i]['AllowRoot'] = true;<br />
//some more text here
Run code snippetHide resultsExpand snippet
In the above we only have to escape the $s to protect them from the shell since the string is enclosed in "s to allow it to include 's.
I'm looking to replace 2 characters in a string only if they are side by side.
Output I have now
hu1.cqf51:qu-2/2
hu2.cqf55:qe-2/2
hu2.cqf41:qe-2/2
The first line is incorrect as it has "qu" instead of "qe"
I'm looking to replace the qu with qe without replacing any other "q" or "e" in the string
Desired output
hu1.cqf51:qe-2/2
hu2.cqf55:qe-2/2
hu2.cqf41:qe-2/2
What I have tried
sed -r 's/[qu]+/qe/g'
sed -e 's//qe/g'
sed 's/\S*\(qu\)\S*//g'
Even considered just trying to delete the whole word alone if it matched with the command below, however it deleted everything with a q or u in it.
sed -e 's/[^ ]*qu[^ ]*//g'
Thank you for your help!!
Can you just try this ?
sed -e 's/qu/qe/' your_file
I'd suggest
sed 's/:qu-/:qe-/'
but if this is a standard format there is probably a standard way.
I want to remove the & from all the line
?daypartId=1&catId=1
?daypartId=1&catId=2
?daypartId=1&catId=11
?daypartId=1&catId=10
?daypartId=1&catId=6
?daypartId=1&catId=4
?daypartId=1&catId=14
?daypartId=1&catId=5
?daypartId=1&catId=3
?daypartId=1&catId=8
Expected output:
?daypartId=1&catId=1
?daypartId=1&catId=2
?daypartId=1&catId=11
?daypartId=1&catId=10
?daypartId=1&catId=6
?daypartId=1&catId=4
?daypartId=1&catId=14
?daypartId=1&catId=5
?daypartId=1&catId=3
?daypartId=1&catId=8
removing & from the input is what i need. I am stuck at this problem please help.
You could do simply with sed like below:
sed 's/amp;//' myfile.txt
This would search for amp; and replace it with an empty string in a file called myfile.txt
If you want to replace it within the file, then you could use -i option as below:
sed -i 's/amp;//' myfile.txt
If you have multiple such occurrences in a line, you could use a global replacement as below:
sed 's/amp;//g' myfile.txt
I have this string of file names.
FileNames="FileName1.txtStrange-File-Name2.txt.zipAnother-FileName.txt"
What I like to do is to separate the file names by semicolon so I can iterate over it. For the .zipextension I have a working command.
I tried the following:
FileNames="${FileNames//.zip/.zip;}"
echo "$FileNames" | sed 's|.txt[^.zip]|.txt;|g'
Which works partially. It add a semicolon to the .zip as expected, but where sed matches the .txt I got the output:
FileName1.txt;trange-File-Name2.txt.zip;Another-FileName.txt
I think because of the character exclusion sed replaces the following character after the match.
I would like to have an output like this:
FileName1.txt;Strange-File-Name2.txt.zip;Another-FileName.txt
I'm not sticked to sed, but it would be fine to using it.
There might be a better way, but you can do it with sed like this:
$ echo "FileName1.txtStrange-File-Name2.txt.zipAnother-FileName.txt" | sed 's/\(zip\|txt\)\([^.]\)/\1;\2/g'
FileName1.txt;Strange-File-Name2.txt.zip;Another-FileName.txt
Beware that [^.zip] matches 'one char that is not ., nor z, nor i nor p'. It does not match 'a word that is not .zip'
Note the less verbose solution by #sundeep:
sed -E 's/(zip|txt)([^.])/\1;\2/g'
sed -r 's/(\.[a-z]{3})(.)/\1;\2/g'
would be a more generic expression.
How to replace a line / part of a line in a file using SED command?
search_text_1_server=value.env_1.path_to_file
search_text_2_server=value.env_1.path_to_file
search_text_3_server=value.env_1.path_to_file
some_other_key=value.env_1.another_path
Now I want a sed command to find the lines which match the regular expression search_text_{any}_server and then replace env_1 with env_2
Found the regular expression to find the required lines.
^search_text_[a-z_]\*_server.*$
Now how to add the SED syntax to replace
PS : I am not an expert in shell
Your regex is close. You can use:
sed -E 's/^(search_text_[a-z_]*_server=.*)env_1\./\1env_2\./' file
search_text_1_server=value.env_2.path_to_file
search_text_2_server=value.env_2.path_to_file
search_text_3_server=value.env_2.path_to_file
some_other_key=value.env_1.another_path
Assuming country code to be two alphabets, you could do
sed -Ei 's/(search_text_[a-z]{2}_server=value\.)env_1/\1env_2/' file
should do it.
What's happening here
[a-z]{2} checks for two alphabets which make a country code
sed s command is for substitution -> s/pattern/replacement
() selects the matched regex pattern for reuse, Note \1 for reuse
-i is the inplace edit option of sed which makes changes permanent in the file