Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
May of your interest :-) – Lucio Nov 28 '14 at 20:38
up vote 201 down vote accepted
sed -i 's/original/new/g' file.txt


  • 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

share|improve this answer
Ahh, I knew "SED" was the right answer. I've found many answers to this question around, many involving sed, but they often were far more complicated calls. This feels RIGHT. +1. – Doc Aug 5 '14 at 21:31
That will work on OSX as well with a slight modification. You have to add as parameter the extension of a backup file. For example: sed -i '.bak' 's/original/new/g' file.txt – Elad Mar 5 '15 at 12:20
@cscarney you are right, just thought it's worth mentioning it for folks like me who try to have it working on their mac. – Elad Mar 6 '15 at 21:47
@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
@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

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
share|improve this answer
note the cat in cat file | sed '...' is unnecessary. You can directly say sed '...' file. – fedorqui Oct 9 '15 at 11:54

Through awk's gsub command,

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


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


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'
share|improve this answer

You can use Vim in Ex mode:

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

  2. s substitute

  3. g replace all instances in each line

  4. x save and close

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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