How can I find and replace specific words in a text file using command line?

up vote 805 down vote accepted
sed -i 's/original/new/g' file.txt

Explanation:

  • sed = Stream EDitor
  • -i = in-place (i.e. save back to the original file)
  • The command string:

    • s = the substitute command
    • original = a regular expression describing the word to replace (or just the word itself)
    • new = the text to replace it with
    • g = global (i.e. replace all and not just the first occurrence)
  • file.txt = the file name

  • 2
    @Akiva If you include regex special characters in your search sed will match them. Add a -r flag if you want to use extended REs instead. – cscarney Nov 28 '14 at 17:38
  • 21
    @mcExchange If it's specifically the / character that you need to match, you can just use some other character as the separator (e.g. 's_old/text_new/text_g'). Otherwise, you can put a \ before any of $ * . [ \ ^ to get the literal character. – cscarney Aug 12 '15 at 18:34
  • 3
    @BrianZ As far as the file system is concerned the output of sed is a new file with the same name. It's one of the commonly reported bugs that are not bugs – cscarney Oct 21 '15 at 17:39
  • 12
    The OSX command sed -i '.bak' 's/original/new/g' file.txt can also be run with a zero-length extension sed -i '' 's/original/new/g' file.txt, which will generate no backup. – Kirk Dec 6 '16 at 18:16
  • 8
    MacOS users will have to add ''" after -i as a parameter for -i ed.gs/2016/01/26/os-x-sed-invalid-command-code so that the file will be overwritten. – geoyws May 29 '17 at 9:44

There are a number of different ways to do this. One is using sed and Regex. SED is a Stream Editor for filtering and transforming text. One example is as follows:

marco@imacs-suck: ~$ echo "The slow brown unicorn jumped over the hyper sleeping dog" > orly
marco@imacs-suck: ~$ sed s/slow/quick/ < orly > yarly
marco@imacs-suck: ~$ cat yarly
The quick brown unicorn jumped over the hyper sleeping dog

Another way which may make more sense than < strin and > strout is with pipes!

marco@imacs-suck: ~$ cat yarly | sed s/unicorn/fox/ | sed s/hyper/lazy/ > nowai
marco@imacs-suck: ~$ cat nowai 
The quick brown fox jumped over the lazy sleeping dog
  • 5
    note the cat in cat file | sed '...' is unnecessary. You can directly say sed '...' file. – fedorqui Oct 9 '15 at 11:54
  • 1
    Indeed this can be reduced further: sed -i'.bak' -e 's/unicorn/fox/g;s/hyper/brown/g' yarly will take file yarly and do the 2 changes in-place whilst making a backup. Using time bash -c "$COMMAND" to time it suggests that this version is a ~5 times faster. – pbhj Oct 22 '17 at 20:14

Through awk's gsub command,

awk '{gsub(/pattern/,"replacement")}' file

Example:

awk '{gsub(/1/,"0");}' file

In the above example, all the 1's are replaced by 0's irrespective of the column where it located.


If you want to done a replacement on a specific column then do like this,

awk '{gsub(/pattern/,"replacement",column_number)}' file

Example:

awk '{gsub(/1/,"0",$1);}' file

It replaces 1 with 0 on the column 1 only.

Through Perl,

$ echo 'foo' | perl -pe 's/foo/bar/g'
bar
  • I used this on MacOS terminal and it did nothing... – Jim Sep 12 at 14:46
  • Tested on Alpine Linux (in Docker container) and got no output – Salathiel Genèse Nov 22 at 8:46
  • @SalathielGenèse what are you trying to achieve? – Avinash Raj Nov 22 at 9:05
  • I'm watching file with inotifywait under sh env, and reporting data in CSV format (because custom format is buggy). I then figured there is no simple way to handle CSV document in shell scripts... And I want it very light. So I started a quite simple script to parse and report CSV. I read CSV spec and noticed it is more elaborated than I expected and support multiline value wrapped in double quotes. I was relying on sed for tokenization but soon realized that even what sed call multilines is up to two lines. What then if one of my CSV values spans on more than two lines? – Salathiel Genèse Nov 22 at 9:15
  • better to ask your problem as question. – Avinash Raj Nov 22 at 9:17

You can use Vim in Ex mode:

ex -s -c '%s/OLD/NEW/g|x' file
  1. % select all lines

  2. s substitute

  3. g replace all instances in each line

  4. x write if changes have been made (they have) and exit

There's multitude of ways to achieve it. Depending on the complexity of what one tries to achieve with string replacement, and depending on tools with which user is familiar, some methods may be preferred more than others.

In this answer I am using simple input.txt file, which you can use to test all examples provided here. The file contents:

roses are red , violets are blue
This is an input.txt and this doesn't rhyme

BASH

Bash isn't really meant for text processing, but simple substitutions can be done via parameter expansion , in particular here we can use simple structure ${parameter/old_string/new_string}.

#!/bin/bash
while IFS= read -r line
do
    case "$line" in
       *blue*) printf "%s\n" "${line/blue/azure}" ;;
       *) printf "%s\n" "$line" ;;
    esac
done < input.txt

This small script doesn't do in-place replacement, meaning that you would have to save new text to new file, and get rid of the old file, or mv new.txt old.txt

Side note: if you're curious about why while IFS= read -r ; do ... done < input.txt is used, it's basically shell's way of reading file line by line. See this for reference.

AWK

AWK, being a text processing utility, is quite appropriate for such task. It can do simple replacements and much more advanced ones based on regular expressions. It provides two functions: sub() and gsub(). The first one only replaces only the first occurrence, while the second - replaces occurrences in whole string. For instance, if we have string one potato two potato , this would be the result:

$ echo "one potato two potato" | awk '{gsub(/potato/,"banana")}1'
one banana two banana

$ echo "one potato two potato" | awk '{sub(/potato/,"banana")}1'                                      
one banana two potato 

AWK can take an input file as argument, so doing same things with input.txt , would be easy:

awk '{sub(/blue/,"azure")}1' input.txt

Depending on the version of AWK you have, it may or may not have in-place editing, hence the usual practice is save and replace new text. For instance something like this:

awk '{sub(/blue/,"azure")}1' input.txt > temp.txt && mv temp.txt input.txt

SED

Sed is a line editor. It also uses regular expressions, but for simple substitutions it's sufficient to do:

sed 's/blue/azure/' input.txt

What's good about this tool is that it has in-place editing, which you can enable with -i flag.

Perl

Perl is another tool which is often used for text processing, but it's a general purpose language, and is used in networking, system administration, desktop apps, and many other places. It borrowed a lot of concepts/features from other languages such as C,sed,awk, and others. Simple substitution can be done as so:

perl -pe 's/blue/azure/' input.txt

Like sed, perl also has the -i flag.

Python

This language is very versatile and is also used in a wide variety of applications. It has a lot of functions for working with strings, among which is replace(), so if you have variable like var="Hello World" , you could do var.replace("Hello","Good Morning")

Simple way to read file and replace string in it would be as so:

python -c "import sys;lines=sys.stdin.read();print lines.replace('blue','azure')" < input.txt

With Python, however, you also need to output to new file , which you can also do from within the script itself. For instance, here's a simple one:

#!/usr/bin/env python
import sys
import os
import tempfile

tmp=tempfile.mkstemp()

with open(sys.argv[1]) as fd1, open(tmp[1],'w') as fd2:
    for line in fd1:
        line = line.replace('blue','azure')
        fd2.write(line)

os.rename(tmp[1],sys.argv[1])

This script is to be called with input.txt as command-line argument.

Python can also have regular expressions , in particular, there's re module, which has re.sub() function, which can be used for more advanced replacements.

sed is the stream editor, in that you can use | (pipe) to send standard streams (STDIN and STDOUT specifically) through sed and alter them programmatically on the fly, making it a handy tool in the Unix philosophy tradition; but can edit files directly, too, using the -i parameter mentioned below.
Consider the following:

sed -i -e 's/few/asd/g' hello.txt

s/ is used to substitute the found expression few with asd:

The few, the brave.


The asd, the brave.

/g stands for "global", meaning to do this for the whole line. If you leave off the /g (with s/few/asd/, there always needs to be three slashes no matter what) and few appears twice on the same line, only the first few is changed to asd:

The few men, the few women, the brave.


The asd men, the few women, the brave.

This is useful in some circumstances, like altering special characters at the beginnings of lines (for instance, replacing the greater-than symbols some people use to quote previous material in email threads with a horizontal tab while leaving a quoted algebraic inequality later in the line untouched), but in your example where you specify that anywhere few occurs it should be replaced, make sure you have that /g.

The following two options (flags) are combined into one, -ie:

-i option is used to edit in place on the file hello.txt.

-e option indicates the expression/command to run, in this case s/.

Note: It's important that you use -i -e to search/replace. If you do -ie, you create a backup of every file with the letter 'e' appended.

You can do like this:

locate <part of filaname to locate> | xargs sed -i -e "s/<Old text>/<new text>/g" 

Examples: to replace all occurrences [logdir', ''] (without [] ) with [logdir', os.getcwd()] in all files that are result of locate command, do:

ex1:

locate tensorboard/program.py | xargs sed -i -e "s/old_text/NewText/g"

ex2:

locate tensorboard/program.py | xargs sed -i -e "s/logdir', ''/logdir', os.getcwd()/g"

where [tensorboard/program.py] is file to search

  • Hi. Your choice of strings (logdir', '' -> /logdir', os.getcwd()) makes this answer hard to parse. Also, it's worth specifying that your answer first locates the files to use sed on, because it's not part of the question. – mwfearnley Aug 22 at 8:05
  • Hi, this answer is both search and replace all if it found <old text> in the file. – Nguyễn Tuấn Anh Aug 24 at 2:11
  • I choose this answer for all they use tensorboard in keras, who want to change command from: tensorboard --logdir='/path/to/log/folder/' to use: tensorboard only, when staying in logs folder. it is very convenient – Nguyễn Tuấn Anh Aug 24 at 2:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.