This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 7 months ago.
I have the below string
sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$
I want replace .*app\.com$|localhost$|127\.0\.0\.1$ with * so that final string looks like below
sec.val.hos.patn=*
I am trying to solve this problem using below sed command on Mac OS
sed -i ' ' 's~\.\*app\\\.com\$\|localhost\$\|127\\\.0\\\.0\\\.1\$~\*~g' file.txt
but unable to get the desired replacement. Can someone please help me to get this working.
I don't know if the pattern is actually more complex than you sample, but seems this should do the trick:
sed 's/\(^.*=\).*$/\1*/' <<< "sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$"
Here we capture everything from the start to =, then we replace the whole thing with capture group 1 plus *.
Related
This question already has answers here:
Using Sed to capitalize the first letter of each word
(4 answers)
Closed 1 year ago.
I have some strings
some-string
some-other-string
yet-another-string-to-handle
I want to convert those strings into
someString
someOtherString
yetAnotherStringToHandle
I'm trying to do the following
echo yet-another-string-to-handle | sed -r 's/\-(.*)/\U\1\E/g'
But that results in
yetANOTHER-STRING-TO-HANDLE
Needless to say, I'm a bit lost. Any suggestions on how I can achieve my goal?
With GNU sed:
sed -E 's/-(.)/\u\1/g' file
\u: Turn the next character to uppercase (GNU 'sed' extension).
Output:
someString
someOtherString
yetAnotherStringToHandle
See: info sed
This question already has answers here:
Replacing first occurence in every line
(6 answers)
unix tr find and replace
(4 answers)
Closed 3 years ago.
I'm trying to use tr in bash to replace only the final character of a match. The string will have a substring with 5 digits followed by a dash, but I want to replace that dash with a slash.
I want to use something like this:
echo "xyzvb12345-Ab-C5678-dEf" | tr "#####-" "#####/"
To get an output like this:
xyzvb12345/Ab-C5678-dEf
Is there a way to do this with tr? Or maybe sed?
EDIT:
This is not a duplicate of the many tickets out there that merely find and replace text. Please read carefully before marking as a duplicate.
echo "xyzvb12345-Ab-C5678-dEf" | sed 's/\([0-9]\{5\}\)-/\1\//g'
[0-9] matches numbers
\{5\} matches five of the previous group (numers)
\(...\) set the matching group so as to be referred in replacement (as \1)
g at the end tells sed to replace all matches in the input
echo "xyzvb12345-Ab-C5678-dEf" | sed '0,/-/s//\//'
Thanks to this other answer
.
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 4 years ago.
I have a file that contains names of directories and some other information, but the names always come first.The file looks like this:
/home/user/Desktop/IS/proj_1/sch/text 4 2018-03-14 07:41:01
/home/user/Desktop/IS/file1.txt 3 2018-03-14 16:50:01
...
I have a variable "name" that contains this for example:
/home/user/Desktop/IS/file1.txt
And I need to delete that one particular line from the file somehow. I've searched many posts and tried using various quotations and ways of expansions, but nothing did the trick. If I type it in directly, it deletes the line without problem, but I'm having a hard time doing it from a variable. This is what I came up with but it still doesn't work.
sed -i '/"$name"/d' $File_name
Try this :
sed -i "\|$name|d" "$File_name"
As you can see, I changed the delimiter for |, you can pick another one depending of your needs from most of ascii characters (not all works)
sed command doesn't allow plain string based search and performs search using only a regex (BRE or ERE). That requires escaping all special regex meta-characters in search pattern.
Better to use a non-regex approach using awk:
name='/home/user/Desktop/IS/file1.txt'
awk -v p="$name" '!index($0, p)' file
/home/user/Desktop/IS/proj_1/sch/text 4 2018-03-14 07:41:01
Whatever given in single quotes wont get expanded.
Try:
sed -i "/$name/d" $File_name
If you have problems with /, escape them properly.
name=$(echo "$name"|sed -e "s/\//\\\\\//g")
sed -i "/$name/d" $File_name
This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 5 years ago.
I have a file and from this file I am trying to find a word and replace it with another word using Bash. I am using sed to do this and please note that the word that I am looking for is an output from a command. So I am trying to find a word, which is the output of a command, and replace it with another word and override the previous word.
This is my code:
File=file.txt
File2=file2.txt
min=$(cat $File2 | grep word);
sed -i 's/$min/max/g' $File
It's not producing any error, but I am unable to find the word in order to replace it. When I manually type the word rather than using the variable "$min" it works just fine. So when I do this, it works:
sed -i 's/min/max/g' $File
but when I do this, it doesn't:
sed -i 's/$min/max/g' $File
I am thinking maybe sed doesn't accept variables as a search string. Any idea how I can achieve this?
thank you.
Use double quotes for the sed expression, this should work:
sed -i "s/$min/max/g" $File
This question already has answers here:
Trying to escape a / for sed on ubuntu
(4 answers)
Closed 9 years ago.
I'm attempting to use SED through OS X Terminal to perform a find and replace.
Imagine I have this string littered throughout the text file: http://www.find.com/page
And I want to replace it with this string: http://www.replace.com/page
I'm having trouble because I'm not sure how to properly escape or use the "/" character in my strings. For example if I simply wanted to find "cat" and replace with "dog" I've found the following command that works perfectly:
sed -i '' 's/cat/dog/g' file.txt
Does anyone have any ideas on how to achieve the same functionality only instead of cat and dog have strings or URLs that container the "/" character? I tried many different ways of escaping the "/" characters but then it seems as if SED can no longer "find" the string and it doesn't perform any find & replace actions.
Any help or tips are greatly appreciated.
Thanks!
/ is not the delimiter in sed commands, it's just one of the possible ones. For this example, you can for example use , instead since it does not conflict with your strings;
echo 'I think http://www.find.com/page is my favorite' |
sed 's,http://www.find.com/page,http://www.replace.com/page,g'
sed can take whatever follows the "s" as the separator. Since you are working with URL it is a good practice to use a different delimiter other than / to not confuse sed when your substitution ends and replacement begins.
However, having said that you can definitely use / if you wish too. You just need to escape the literal /.
So, you can either do:
sed 's/http:\/\/www.find.com\/page/http:\/\/www.replace.com\/page/g' input_file
or use a different delimiter to avoid making your cryptic sed more cryptic.
sed 's#http://www.find.com/page#http://www.replace.com/page#g' input_file