Replace in linux commands

Linux replace command

On Unix-like operating systems, the replace command makes modifications to strings of text in files or the standard input.

Description

replace looks for all occurrences of string from and replaces it with string to. You can specify one or more pairs of strings to search/replace in a single replace command.

Use the option to indicate where the string-replacement list ends and the file names begin. In this case, any file named on the command line is modified in place, so you may want to make a copy of the original before converting it. replace prints a message indicating which of the input files actually got modifies.

If the option is not given, replace reads the standard input and writes to the standard output.

Syntax

Options

-?, -I Display a help message and exit.
-#debug_options Enable debugging.
-s Silent mode. Print less information what the program does.
-v Verbose mode. Print more information about what the program does.
-V Display version information and exit.

Examples

The replace command is primarily used with msql2mysql although can still be used for a way to replace text within a file or input. If you are looking for a tool in Linux to replace text in one or more files, try the sed («stream editor») command.

replace «computer» «hope» — example.txt

Replace the word computer with the word hope in example.txt, overwriting the original file with the changes.

sed — A utility for filtering and transforming text.

Источник

How to use sed to find and replace text in files in Linux / Unix shell

Find and replace text within a file using sed command

The procedure to change the text in files under Linux/Unix using sed:

  1. Use Stream EDitor (sed) as follows:
  2. sed -i ‘s/old-text/new-text/g’ input.txt
  3. The s is the substitute command of sed for find and replace
  4. It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt
  5. Verify that file has been updated:
  6. more input.txt

Let us see syntax and usage in details.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed utility on Linux, macOS or Unix-like OS
Est. reading time 4 minutes

Syntax: sed find and replace text

The syntax is:
sed ‘s/word1/word2/g’ input.file
## *bsd/macos sed syntax#
sed ‘s/word1/word2/g’ input.file > output.file
sed -i ‘s/word1/word2/g’ input.file
sed -i -e ‘s/word1/word2/g’ -e ‘s/xx/yy/g’ input.file
## use + separator instead of / ##
sed -i ‘s+regex+new-text+g’ file.txt
The above replace all occurrences of characters in word1 in the pattern space with the corresponding characters from word2.

Examples that use sed to find and replace

Let us create a text file called hello.txt as follows:
$ cat hello.txt
The is a test file created by nixCrft for demo purpose.
foo is good.
Foo is nice.
I love FOO.

I am going to use s/ for substitute the found expression foo with bar as follows:
sed ‘s/foo/bar/g’ hello.txt
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Please note that the BSD implementation of sed (FreeBSD/MacOS and co) does NOT support case-insensitive matching. You need to install gnu sed. Run the following command on Apple Mac OS:
$ brew install gnu-sed
######################################
### now use gsed command as follows ##
######################################
$ gsed -i ‘s/foo/bar/g I ‘ hello.txt
$ cat hello.txt

sed command problems

Consider the following text file:
$ cat input.txt
http:// is outdate.
Consider using https:// for all your needs.

Find word ‘http://’ and replace with ‘https://www.cyberciti.biz’:
sed ‘s/ http:// / https://www.cyberciti.biz /g’ input.txt
You will get an error that read as follows:

Our syntax is correct but the / delimiter character is also part of word1 and word2 in above example. Sed command allows you to change the delimiter / to something else. So I am going to use +:
sed ‘s+ http:// + https://www.cyberciti.biz +g’ input.txt
Sample outputs:

How to use sed to match word and perform find and replace

In this example only find word ‘love’ and replace it with ‘sick’ if line content a specific string such as FOO:
sed -i -e ‘/FOO/s/love/sick/’ input.txt
Use cat command to verify new changes:
cat input.txt

Recap and conclusion – Using sed to find and replace text in given files

The general syntax is as follows:
## find word1 and replace with word2 using sed ##
sed -i ‘s/word1/word2/g’ input
## you can change the delimiter to keep syntax simple ##
sed -i ‘s+word1+word2+g’ input
sed -i ‘s_word1_word2_g’ input
## you can add I option to GNU sed to case insensitive search ##
sed -i ‘s/word1/word2/gI’ input
sed -i ‘s_word1_word2_gI’ input

See BSD(used on macOS too) sed or GNU sed man page by typing the following command:
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

replace — Unix, Linux Command

SYNOPSIS

Tag Description
replace arguments

DESCRIPTION

The replace utility program changes strings in place in files or on the standard input.

Invoke replace in one of the following ways:

from represents a string to look for and to represents its replacement. There can be one or more pairs of strings.

Use the option to indicate where the string-replacement list ends and the file names begin. In this case, any file named on the command line is modified in place, so you may want to make a copy of the original before converting it. replace prints a message indicating which of the input files it actually modifies.

If the option is not given, replace reads the standard input and writes to the standard output.

replace uses a finite state machine to match longer strings first. It can be used to swap strings. For example, the following command swaps a and b in the given files, file1 and file2:

The replace program is used by msql2mysql. See msql2mysql(1).

replace supports the following options.

Tag Description
o -?, -I

Display a help message and exit.

Tag Description
o -# debug_options

Enable debugging.

Tag Description
o -s

Silent mode. Print less information what the program does.

Tag Description
o -v

Verbose mode. Print more information about what the program does.

Tag Description
o -V

Display version information and exit.

Copyright © 1997, 2011, Oracle and/or its affiliates. All rights reserved.

This documentation is free software; you can redistribute it and/or modify it only under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This documentation is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Источник

Bash Shell: Replace a String With Another String In All Files Using sed and Perl -pie Options

/foo directory has 100s of text file and I would like to find out xyz string and replace with abc. I need to to use sed or any other tool to replace all occurrence of the word under Linux and Unix-like operating systems. How do we replace words in files?

How to use sed command to replace a string with another string

The syntax is as follows:
sed -i ‘s/ old-word / new-word /g’ *.txt
GNU sed command can edit files in place (makes backup if extension supplied) using the -i option. If you are using an old UNIX sed command version try the following syntax:
sed ‘s/ old / new /g’ input.txt > output.txt
The — option also know as replace in place. We can use old sed syntax along with bash for loop as follows:

A Note About Bash Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path “/nfs/apache/logs/rawlogs/access.log” with “__DOMAIN_LOG_FILE__”:

The $_r1 is escaped using bash find and replace parameter substitution syntax to replace each occurrence of / with \/.

Replace one substring for another string in shell

Say you need to find ‘/foo/bar’ and replace with ‘/home/bar’. Here is what we do in sed:
sed ‘s+/foo/bar+/home/bar+g’ input
sed ‘s+/foo/bar+/home/bar+g’ input > output
sed -i’.Bak’ ‘s+/foo/bar+/home/bar+g’ input
We used the + instead of / as delimiter to avoid errors. See “how to use sed to find and replace text in files in Linux / Unix shell” for more info.

Replace strings in files with bash string manipulation operators only

No need to use the sed or Perl. We can replace the first occurrence of a pattern/string with a given string. The syntax is for bash version 4.x+:

For instance, replace a string ‘Unix’ with ‘Linux’:

Here is another bash string manipulation example that replaces all occurrences of ‘Linux’ with ‘Unix’:

Replacing a string in a file in Bash variable when using sed

Often our shell scripts have variables, and we need to pass those variables to sed. For instance:

How to replace an array of values using sed

Let us use the cat command to see our current template file that contains AWS RDS info config for PHP app:
cat config_template.txt
Herer is what I have:

My task is to replace ‘ __NAME__ ‘ and other place holder values using bash script:

How to replace a string in a File in Perl

The perl -pi -e syntax to find and replace a string is as follows:
perl -pi -e ‘s/ old-word / new-word /g’ input.file
perl -pi -e ‘s/old/new/g’ *.conf
Where,

  • -p : act like sed
  • -i : edit file in place
  • -e : One line Perl programe

When Perl executed, it will look all files matching *.conf . Then it will run the replacement pattern ‘ s/old/new/g ‘. In other words, we used Perl regular expression to eplaces all occurrences of old with new .

Summing up

In this tutorial, we learned how to use sed, bash, and Perl to find and replace strings in a single or bunch of files. See man pages for more info:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Glancing at this I see a typo:
sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv TFILE “$f”

Should be:
sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv $TFILE “$f”

Also, you didn’t seem to mention that sed can auto-create backup files using syntax like this:
sed -i.bak ‘s/old-word/new-word/g’ filename.txt

That will edit filename.txt and copy the original to filename.txt.bak

thanks for the heads up!

I don’t know normal sed has -i.bak option but perl provides the backup of the original file.

perl -p -i.bak -e ‘s/AAA/aaa/’ file_name

Just a small remark:
perl -pie ‘s/old-word/new-word/g’ input
modifies the input file, so no need for ‘ > output’ like with sed.

Thanks for the great tip. Heres how I did it using a pipe to provide file paths:

find . -name ‘*.xaction’ | xargs sed -i ‘s/test-data/live-data/g’

Thank you, this was useful!

Recursively replace a word in multiple files in multiple directories?
I couldn’t find a recursive option in sed, so …

grep -ilr ‘old-word’ * | xargs -i@ sed -i ‘s/old-word/new-word/g’ @

Which will find each file with the old-word in it, complete with path, then pipe through xargs and run sed.
Does the trick.

@Matthew, that worked perfectly for recursive find and replace. Thanks.

I had to do about 5 things to make this work in OSX Snow Leopard. NOTE THAT COPYING DIRECTLY MIGHT NOT WORK. This is because the single quote characters could turn into curly quotes after posted. What I ended up with is

grep -ilr ‘old-word’ * | xargs -I@ sed -i ” ‘s/old-word/new-word/g’ @

1. Curly single quotes had to become straight single quotes.
2. Lowercase -i doesn’t appear in xargs in this OSX version, had to use uppercase -I (and spent a long time trying to figure out that @ wasn’t the problem because it didn’t have some special meaning).
3. OSX didn’t like the missing backup suffix, it interpreted this to mean that replacement pattern was the suffix and that @ was the command. Now that I think of it, how does any OS get that the pattern isn’t the suffix? Maybe the those were accent marks that were changed into curly single quotes?

Ok, only three things, but it felt like an eternity trying to figure it out. Actually, it was probably over an hour.

Well… thanks for your post. Your hour made it like a 5 min. work to me. Seems like ubuntu has the same issue. I just had to replace the curly apostrophes 😉

Hello,
I’m a linux newbie.
I’m trying to change a string which is a windows path file (e.g. C:\Documents and Settings\path) into a unix path-file (e.g. /home/path). The file containing the string I want to change is a textual file (a bibtex file: file.bib). This files containes the addresses of other files, and they appear as: “C\:\\Documents and Setting\\path\\”). I’m trying with the “sed” command but apparently unix do not like the “\\”. Someone knows the solution?

This wont work when I try to use it for replacing a work having “/” in it.
I use a script to change password for other machine. But once it a while the problem comes like
New password is “dkCHQ4/uTfSIU” old paswd “14eMXYEwUD7Cc”

sed ‘s/14eMXYEwUD7Cc/dkCHQ4/uTfSIU’ /etc/passwd > $newpassfile
it throws error.

You can change the / from the sed option to any char you want. Try this

sed ‘s|14eMXYEwUD7Cc|dkCHQ4/uTfSIU’ /etc/passwd > $newpassfile

I think it’s better to understand then escape all the ‘/’s in the string like Mattheu suggested.

@Suvankar, you need to escape backslashes with forward slashes.

sed ’s/14eMXYEwUD7Cc/dkCHQ4\/uTfSIU’ /etc/passwd > $newpassfile

hi,
i need to remove the SOP in the code. My code contains a call to another function immediately after SOP call. please suggest how can i find the first occurance of ‘;’ in the line and remove the text till first occurance of ‘;’

System.out.println(“06”); String sDebitTxnmemo= cm.getString(“********”);

replace OldString NewString — *.txt

****** BE CAREFUL LOL…. YOU WILL DELETE THE ENTIRE FILE IF IT IS A .CC FILE WITH “sed s/”old”/”new”/g name.cc > name.cc

hi
i want search for a word in a file and if found ,i want to add a word at the begning of that line where the word is found….. is there any command for that?

Hi Nahas,
something like this will do the job

cat the.file|perl -pne ‘s/^(thewordtofind)/TheWordToAdd$1/’

I have a template file, and would like to replace some words in that file with words taken from another file like:
File1
Hi $1 I miss being with you at $2
File2
Johan London
So the output will be a new file contains
Hi Johan I miss being with you at London.

You can read data into variable using cut, sed, awk, and any other shell builtin:

Hi All,
We had a situation to find and replace a word in huge number of files. But if we create a new file with the sed command, it may create space issue. So we are asked to remove the old file once this replacement happens and retain only the modified file.
You may make use of the following script:
> vi change.sh

> nohup sh change.sh (run this in background)
> tail -f Change.log (To track changes)

Thank you for these useful tips. Same question as Suvankar with a twist:
A=/path1
B=/path2
sed -i ‘s/$A/$B/g’ f.txt
throws an error.
I tried escaping with A=\/path1 to no avail.
Any further tip for such a case? Thanks
PS Also, what is the difference between using single and double quotes above? Thanks again.

Should be as follows:

You need to escape / so that sed will not confuse with s/ command. See the difference between single and double quote here:

a] Enclosing characters in single quotes preserves the literal value of each character within the quotes
b] Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘`’ retain their special meaning within double quotes.

That works perfectly (even with spaces in the path). Thank you.
So the single quotes in A=’\/path1′ prevent interpreting the escape character until it is used within double quotes, right?

Now my script has matured a bit. Here is where I am at. This attempts to delete files in a tree if they are absent from another directory tree.

Any clue how to soft-code the paths? I have run out of ideas to try.
Thanks again.

I have a problem could anyone tell me a way to open the access.log file which does not have permission to read or write. I need to read that file for monitoring purpose. I cannot use chmod command to change the permission as it says permission denied. I came to know that there is a shell command to open it. It will be great help if anyone give any idea in perl or php to read that information.

thanks in advance

Shameel,
Use sudo chmod. If you don’t have super user access, then you might not be able to open this file.

I could not understand the following line can u explain its functionality.

if [ -f $f -a -r $f ]; then //explain only this one whole script are below

#!/bin/bash
OLD=”xyz”
NEW=”abc”
DPATH=”/home/you/foo/*.txt”
BPATH=”/home/you/bakup/foo”
TFILE=”/tmp/out.tmp.$$”
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
if [ -f $f -a -r $f ]; then
/bin/cp -f $f $BPATH
sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv $TFILE “$f”
else
echo “Error: Cannot read $f”
fi
done

I have a dictionary file:
sme,replace
me,place

And on the processing file, i have:
123456,sme,89
678901,me,78

Need to replace values in processing file with corresponding values from dictionary file. e.g. sme -> replace

Would appreciate on this.

Hi All,
I am facing an issue wherein i need to replace a word with another one. But my case is a bit different here. Only a part of the word is constant which I know. The other half is variable ( too many possibilities ). I need to change the complete word with a new word. For example
The possible old words may be
abc123ad, abc123bc, abc123ak and so on….
i need to replace any of the above in my file with a new word.

Please help :(.
Thanks in advance.

Thanks,
I guess i have found it…..
sed ‘s/abc123[aa-zz]*/new_word/g’ filename

If any of the files fed through xargs are binary, both sed and perl will change the timestamps of the files that all have the string, binary files or text files, but no changes are made (in at least the one file *I* was looking at at the top of the file directory hierarchy)

So filter out binary files.

Hi All,
I want to replace a string in a file and redirect the output to a new file,but this is slightly tricky.The input file contains data as A|B|C|D|E|F|G|H|I|K , now i want to replace alphabet “F” to “:” and redirect the output file to a file.The final output must look like this:A|B|C|D|E|:|G|H|I|K. Please help it’s urgent

It means i create a database using mysql and create a website using joomla so when i click a link from the web i can directly retrieve the data from mysql server.

I need help immediately. I need to monitor multiple web servers access.log file. I have completed the coding for single server completely but the problem we are facing is let us imagine two servers 192.47.155.200 and 192.47.155.36. The server 192.47.155.200 has database now how to fetch the information from 192.27.155.36 being in that server and insert in that database. I want to create a link between two servers that it does not ask for password everytime fetch the data by itself as i am running cron job. I have access to both the server as i am the admin of it. I am doing coding in php.

Ok , sorry for this stupid question but someone can tell me how i can “delete” “/home/” for example i use

echo $HOME
/home/user_name

i need to delete “/home/” to get “username” without “whoami” or something like that

Источник

Читайте также:  Media creation tools для версии windows 10
Оцените статью