How do I replace multiple text at once in a file on Linux or Mac? - linux

So example, I have the file my.txt as below
This is my xzy.
My color is white.
I want to replace "my" to our, and I also want to replace "xyz" to "abc".
How can I do this using a one line command?

Use sed with multiple replacement:
sed -i 's/my/our/g; s/xyz/abc/g' text.txt

With sed
For GNU sed
sed -i -e 's/my/our/g' -e 's/xyz/abc/g' my.txt
In MacOS, -e option may not be available. -i option in MacOS needs an extension. Use something like this:
sed -i.bak 's/my/our/g;s/xyz/abc/g' my.txt

Related

How to change a windows path to a linux path in all files under a directory using sed

I copied a directory structure from a windows box to a Linux box and I would like to use sed to replace c:\IBM\WebSphere with /opt/IBM/WebSphere in all files under this directory.
Any thoughts?
I think sed is a little inconvenient for that purpose, if you want to change the actual files you can use perl one-liner
perl -pi -e 's/c:\\IBM\\/\/opt\/IBM\//g' *
Add or adjust the paths according to what you need (add WebSphere if you want the replacement to change only these dirs)
Since sed can take any character as a delimiter, use
sed -e 's_\\_/_g'
to replace the \ to /.
sed -e 's_[Cc]:_/opt_g'
to replace the c: with /opt
You can string those together:
echo "C:\\IBM\\WebSphere" | sed -e 's_\\_/_g' -e 's_[Cc]:_/opt_g'
Output:
/opt/IBM/WebSphere
I don't see an awk solution, just add one:
awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1'
test:
kent$ awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1' <<<'c:\IBM\WebSphere'
/opt/IBM/WebSphere
echo "C:\Users\San.Tan\My Folder\project1" | sed -e 's/C:\\/mnt\/c\//;s/\\/\//g'
replaces
C:\Users\San.Tan\My Folder\project1
to
mnt/c/Users/San.Tan/My Folder/project1
in case someone needs to replace windows paths to Windows Subsystem for Linux(WSL) paths

How to delete lines from file with sed\awk?

I have file, with lines, contains ip with netmask
a.b.c.d/24
w.x.y.z/32
etc
How to delete delete specific row?
i'm using
sed -ie "s#a.b.c.d/24##g" %filname%
but after the removal is an empty string in file.
It should run inside a script, with ip as parameter and also work in freebsd under sh.
Sed solution
sed -i '/<pattern-to-match-with-proper-escape>/d' data.txt
-i option will change the original file.
Awk solution
awk '!/<pattern-to-match-with-proper-escape>/' data.txt
Using sed:
sed -i '\|a.b.c.d/24|d' file
Command line arg:
For the input being command line argument, say 1st argument($1):
sed -i "\|$1|d" file
Replace $1 with appropriate argument number as is your case.
You should use d (delete) not g. Also do not use s (replacement).
sed -ie '/a.b.c.d\/24/d' %filename%
In a script you should using it in this way
IP=$1
IPA=${IP////\\/}
sed -i /"${IPA}"/d %filename%
And the script parameter should be called in this way:
./script.sh a.b.c.d/24
perl -i -lne 'print unless(/a.b.c.d\/24/)' your_file
or in awk if you donot want to do inplace editing:
awk '$0!~/a.b.c.d\/24/' your_file

linux shell sed command

I have file sedFile.txt which has string in format CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
I created one script which has following lines:
fin=CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
repla=connection
sed -i "s/\$fin/$repla/g" /home/sedFile.txt
Even though the script is running, it's not doing changes in my file.
I tried following:
sed -i 's/${fin}/${repla}/g' /home/sedFile.txt
sed -i 's/^$fin/$repla/g' /home/sedFile.txt
sed -i "s/$fin/$repla/g" /home/sedFile.txt
sed -i "s/${fin}/${repla}/g" /home/sedFile.txt
If you want the single quotes to be included in the pattern you have to quote or escape them:
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
then, use any of the four lines you tried (i.e. not the one with \$fin).
Update: In order to make sed work, you cannot use / to separate the pattern and the substitution, because this character exists in the string already. Use a different separator:
sed -i "s,$fin,$repla,g" /home/sedFile.txt
Might be the same as the other answers, but I doesn't hurt to try
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';"
repla="connection"
sed -i "s|${fin}|${repla}|g" /home/sedFile.txt
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
repla=connection
in=$fin out=$repla perl -pi.nk -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' /home/sedFile.txt

How to remove a special character in a string in a file using linux commands

I need to remove the character : from a file. Ex: I have numbers in the following format:
b3:07:4d
I want them to be like:
b3074d
I am using the following command:
grep ':' source.txt | sed -e 's/://' > des.txt
I am new to Linux. The file is quite big & I want to make sure I'm using the write command.
You can do without the grep:
sed -e 's/://g' source.txt > des.txt
The -i option edits the file in place.
sed -i 's/://' source.txt
the first part isn't right as it'll completely omit lines which don't contain :
below is untested but should be right. The g at end of the regex is for global, means it should get them all.
sed -e 's/://g' source.txt > out.txt
updated to better syntax from Jon Lin's answer but you still want the /g I would think

Find and replace in shell scripting

Is it possible to search in a file using shell and then replace a value? When I install a service I would like to be able to search out a variable in a config file and then replace/insert my own settings in that value.
Sure, you can do this using sed or awk. sed example:
sed -i 's/Andrew/James/g' /home/oleksandr/names.txt
You can use sed to perform search/replace. I usually do this from a bash shell script, and move the original file containing values to be substituted to a new name, and run sed writing the output to my original file name like this:
#!/bin/bash
mv myfile.txt myfile.txt.in
sed -e 's/PatternToBeReplaced/Replacement/g' myfile.txt.in > myfile.txt.
If you don't specify an output, the replacement will go to stdout.
sed -i 's/variable/replacement/g' *.conf
You can use sed to do this:
sed -i 's/toreplace/yoursetting/' configfile
sed is probably available on every unix like system out there. If you want to replace more than one occurence you can add a g to the s-command:
sed -i 's/toreplace/yoursetting/g' configfile
Be careful since this can completely destroy your configfile if you don't specify your toreplace-value correctly. sed also supports regular expressions in searching and replacing.
Look at the UNIX power tools awk, sed, grep and in-place edit of files with Perl.
filepath="/var/start/system/dir1"
searchstring="test"
replacestring="test01"
i=0;
for file in $(grep -l -R $searchstring $filepath)
do
cp $file $file.bak
sed -e "s/$searchstring/$replacestring/ig" $file > tempfile.tmp
mv tempfile.tmp $file
let i++;
echo "Modified: " $file
done
Generally a tool like awk or sed are used for this.
$ sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt

Resources