Find and replace text within a file using commandsAdd/edit line text in file without open editor (linux command)edit json file using scriptReplacing a line with SED not workingReplace a string with other onereplace string in a fileChanging text on a file using scriptsHow do I change mirrors in Ubuntu Server from regional to main?Change 2 or more LibreOffice documents to have the exact same styling/formattingusing sed replace the new line with characterHow to arrange this file properly?Add words to a text file using a single terminal command (no editors)Edit text iteratively within a file using commandsFind and Replace with BashFind and replace with on PDF file from command lineFinding file and replacing word with some specific wordFind a file and delete some lines within itHow do I find a word and add text after it in a .txt file?How to replace text randomly from file?Find/Replace special characters in text file using Bash scriptFind & Replace in 80mb file?

Can a Cauchy sequence converge for one metric while not converging for another?

dbcc cleantable batch size explanation

Languages that we cannot (dis)prove to be Context-Free

Convert two switches to a dual stack, and add outlet - possible here?

What's the point of deactivating Num Lock on login screens?

What does it mean to describe someone as a butt steak?

Important Resources for Dark Age Civilizations?

How to move a thin line with the black arrow in Illustrator?

Codimension of non-flat locus

How does one intimidate enemies without having the capacity for violence?

Is it possible to run Internet Explorer on OS X El Capitan?

Why do I get two different answers for this counting problem?

Decision tree nodes overlapping with Tikz

Do infinite dimensional systems make sense?

Two films in a tank, only one comes out with a development error – why?

Arrow those variables!

Alternative to sending password over mail?

Can I make popcorn with any corn?

Cross compiling for RPi - error while loading shared libraries

Watching something be written to a file live with tail

I'm flying to France today and my passport expires in less than 2 months

Why does Kotter return in Welcome Back Kotter?

What does the "remote control" for a QF-4 look like?

Was any UN Security Council vote triple-vetoed?



Find and replace text within a file using commands


Add/edit line text in file without open editor (linux command)edit json file using scriptReplacing a line with SED not workingReplace a string with other onereplace string in a fileChanging text on a file using scriptsHow do I change mirrors in Ubuntu Server from regional to main?Change 2 or more LibreOffice documents to have the exact same styling/formattingusing sed replace the new line with characterHow to arrange this file properly?Add words to a text file using a single terminal command (no editors)Edit text iteratively within a file using commandsFind and Replace with BashFind and replace with on PDF file from command lineFinding file and replacing word with some specific wordFind a file and delete some lines within itHow do I find a word and add text after it in a .txt file?How to replace text randomly from file?Find/Replace special characters in text file using Bash scriptFind & Replace in 80mb file?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








553















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










share|improve this question



















  • 1





    May of your interest github.com/lucio-martinez/rch :-)

    – Lucio
    Nov 28 '14 at 20:38

















553















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










share|improve this question



















  • 1





    May of your interest github.com/lucio-martinez/rch :-)

    – Lucio
    Nov 28 '14 at 20:38













553












553








553


204






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










share|improve this question
















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







command-line text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 '14 at 20:24









Kaz Wolfe

26.1k1376136




26.1k1376136










asked Jan 7 '11 at 4:10









Jon DoeJon Doe

3,29951612




3,29951612







  • 1





    May of your interest github.com/lucio-martinez/rch :-)

    – Lucio
    Nov 28 '14 at 20:38












  • 1





    May of your interest github.com/lucio-martinez/rch :-)

    – Lucio
    Nov 28 '14 at 20:38







1




1





May of your interest github.com/lucio-martinez/rch :-)

– Lucio
Nov 28 '14 at 20:38





May of your interest github.com/lucio-martinez/rch :-)

– Lucio
Nov 28 '14 at 20:38










7 Answers
7






active

oldest

votes


















913














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






share|improve this answer




















  • 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







  • 25





    @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







  • 14





    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






  • 11





    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


















30














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


















  • 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


















20














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





share|improve this answer

























  • I used this on MacOS terminal and it did nothing...

    – Jim
    Sep 12 '18 at 14:46











  • Tested on Alpine Linux (in Docker container) and got no output

    – Salathiel Genèse
    Nov 22 '18 at 8:46











  • @SalathielGenèse what are you trying to achieve?

    – Avinash Raj
    Nov 22 '18 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 '18 at 9:15











  • better to ask your problem as question.

    – Avinash Raj
    Nov 22 '18 at 9:17


















18














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






share|improve this answer
































    14














    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 "%sn" "$line/blue/azure" ;;
    *) printf "%sn" "$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. The exact command to run python script with command-line argument would be



     $ ./myscript.py input.txt


    or



    $ python ./myscript.py input.txt


    Of course, make sure that ./myscript.py is in your current working directory and for the first way, ensure it is set executable with chmod +x ./myscript.py



    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.






    share|improve this answer
































      8
















      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.






      share|improve this answer






























        1














        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






        share|improve this answer

























        • 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 '18 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 '18 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 '18 at 2:27









        protected by Community Jan 4 at 14:52



        Thank you for your interest in this question.
        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



        Would you like to answer one of these unanswered questions instead?














        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        913














        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






        share|improve this answer




















        • 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







        • 25





          @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







        • 14





          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






        • 11





          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















        913














        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






        share|improve this answer




















        • 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







        • 25





          @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







        • 14





          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






        • 11





          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













        913












        913








        913







        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






        share|improve this answer















        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 17 '14 at 23:02









        kiri

        19.3k1360106




        19.3k1360106










        answered Jan 7 '11 at 4:23









        cscarneycscarney

        12.7k12126




        12.7k12126







        • 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







        • 25





          @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







        • 14





          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






        • 11





          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












        • 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







        • 25





          @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







        • 14





          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






        • 11





          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







        2




        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






        @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





        25




        25





        @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






        @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




        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






        @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





        14




        14





        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





        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




        11




        11





        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





        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













        30














        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


















        • 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















        30














        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


















        • 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













        30












        30








        30







        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













        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












        share|improve this answer



        share|improve this answer










        answered Jan 7 '11 at 4:26









        Marco CeppiMarco Ceppi

        37.2k24155192




        37.2k24155192







        • 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












        • 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







        5




        5





        note the cat in cat file | sed '...' is unnecessary. You can directly say sed '...' file.

        – fedorqui
        Oct 9 '15 at 11:54





        note the cat in cat file | sed '...' is unnecessary. You can directly say sed '...' file.

        – fedorqui
        Oct 9 '15 at 11:54




        1




        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





        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











        20














        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





        share|improve this answer

























        • I used this on MacOS terminal and it did nothing...

          – Jim
          Sep 12 '18 at 14:46











        • Tested on Alpine Linux (in Docker container) and got no output

          – Salathiel Genèse
          Nov 22 '18 at 8:46











        • @SalathielGenèse what are you trying to achieve?

          – Avinash Raj
          Nov 22 '18 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 '18 at 9:15











        • better to ask your problem as question.

          – Avinash Raj
          Nov 22 '18 at 9:17















        20














        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





        share|improve this answer

























        • I used this on MacOS terminal and it did nothing...

          – Jim
          Sep 12 '18 at 14:46











        • Tested on Alpine Linux (in Docker container) and got no output

          – Salathiel Genèse
          Nov 22 '18 at 8:46











        • @SalathielGenèse what are you trying to achieve?

          – Avinash Raj
          Nov 22 '18 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 '18 at 9:15











        • better to ask your problem as question.

          – Avinash Raj
          Nov 22 '18 at 9:17













        20












        20








        20







        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





        share|improve this answer















        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 28 '14 at 4:58

























        answered Jul 2 '14 at 12:59









        Avinash RajAvinash Raj

        52.7k41171219




        52.7k41171219












        • I used this on MacOS terminal and it did nothing...

          – Jim
          Sep 12 '18 at 14:46











        • Tested on Alpine Linux (in Docker container) and got no output

          – Salathiel Genèse
          Nov 22 '18 at 8:46











        • @SalathielGenèse what are you trying to achieve?

          – Avinash Raj
          Nov 22 '18 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 '18 at 9:15











        • better to ask your problem as question.

          – Avinash Raj
          Nov 22 '18 at 9:17

















        • I used this on MacOS terminal and it did nothing...

          – Jim
          Sep 12 '18 at 14:46











        • Tested on Alpine Linux (in Docker container) and got no output

          – Salathiel Genèse
          Nov 22 '18 at 8:46











        • @SalathielGenèse what are you trying to achieve?

          – Avinash Raj
          Nov 22 '18 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 '18 at 9:15











        • better to ask your problem as question.

          – Avinash Raj
          Nov 22 '18 at 9:17
















        I used this on MacOS terminal and it did nothing...

        – Jim
        Sep 12 '18 at 14:46





        I used this on MacOS terminal and it did nothing...

        – Jim
        Sep 12 '18 at 14:46













        Tested on Alpine Linux (in Docker container) and got no output

        – Salathiel Genèse
        Nov 22 '18 at 8:46





        Tested on Alpine Linux (in Docker container) and got no output

        – Salathiel Genèse
        Nov 22 '18 at 8:46













        @SalathielGenèse what are you trying to achieve?

        – Avinash Raj
        Nov 22 '18 at 9:05





        @SalathielGenèse what are you trying to achieve?

        – Avinash Raj
        Nov 22 '18 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 '18 at 9:15





        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 '18 at 9:15













        better to ask your problem as question.

        – Avinash Raj
        Nov 22 '18 at 9:17





        better to ask your problem as question.

        – Avinash Raj
        Nov 22 '18 at 9:17











        18














        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






        share|improve this answer





























          18














          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






          share|improve this answer



























            18












            18








            18







            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






            share|improve this answer















            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 5 '18 at 4:08

























            answered Apr 16 '16 at 18:36









            Steven PennySteven Penny

            1




            1





















                14














                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 "%sn" "$line/blue/azure" ;;
                *) printf "%sn" "$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. The exact command to run python script with command-line argument would be



                 $ ./myscript.py input.txt


                or



                $ python ./myscript.py input.txt


                Of course, make sure that ./myscript.py is in your current working directory and for the first way, ensure it is set executable with chmod +x ./myscript.py



                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.






                share|improve this answer





























                  14














                  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 "%sn" "$line/blue/azure" ;;
                  *) printf "%sn" "$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. The exact command to run python script with command-line argument would be



                   $ ./myscript.py input.txt


                  or



                  $ python ./myscript.py input.txt


                  Of course, make sure that ./myscript.py is in your current working directory and for the first way, ensure it is set executable with chmod +x ./myscript.py



                  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.






                  share|improve this answer



























                    14












                    14








                    14







                    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 "%sn" "$line/blue/azure" ;;
                    *) printf "%sn" "$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. The exact command to run python script with command-line argument would be



                     $ ./myscript.py input.txt


                    or



                    $ python ./myscript.py input.txt


                    Of course, make sure that ./myscript.py is in your current working directory and for the first way, ensure it is set executable with chmod +x ./myscript.py



                    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.






                    share|improve this answer















                    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 "%sn" "$line/blue/azure" ;;
                    *) printf "%sn" "$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. The exact command to run python script with command-line argument would be



                     $ ./myscript.py input.txt


                    or



                    $ python ./myscript.py input.txt


                    Of course, make sure that ./myscript.py is in your current working directory and for the first way, ensure it is set executable with chmod +x ./myscript.py



                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 58 secs ago

























                    answered Feb 3 '17 at 7:49









                    Sergiy KolodyazhnyySergiy Kolodyazhnyy

                    74.9k9155326




                    74.9k9155326





















                        8
















                        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.






                        share|improve this answer



























                          8
















                          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.






                          share|improve this answer

























                            8












                            8








                            8









                            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.






                            share|improve this answer















                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '17 at 9:00









                            Chaminda BandaraChaminda Bandara

                            225210




                            225210





















                                1














                                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






                                share|improve this answer

























                                • 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 '18 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 '18 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 '18 at 2:27















                                1














                                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






                                share|improve this answer

























                                • 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 '18 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 '18 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 '18 at 2:27













                                1












                                1








                                1







                                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






                                share|improve this answer















                                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







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 19 '18 at 2:45

























                                answered Jul 24 '18 at 2:13









                                Nguyễn Tuấn AnhNguyễn Tuấn Anh

                                112




                                112












                                • 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 '18 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 '18 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 '18 at 2:27

















                                • 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 '18 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 '18 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 '18 at 2:27
















                                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 '18 at 8:05





                                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 '18 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 '18 at 2:11





                                Hi, this answer is both search and replace all if it found <old text> in the file.

                                – Nguyễn Tuấn Anh
                                Aug 24 '18 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 '18 at 2:27





                                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 '18 at 2:27





                                protected by Community Jan 4 at 14:52



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?



                                Popular posts from this blog

                                Are there any comparative studies done between Ashtavakra Gita and Buddhim?How is it wrong to believe that a self exists, or that it doesn't?Can you criticise or improve Ven. Bodhi's description of MahayanaWas the doctrine of 'Anatta', accepted as doctrine by modern Buddhism, actually taught by the Buddha?Relationship between Buddhism, Hinduism and Yoga?Comparison of Nirvana, Tao and Brahman/AtmaIs there a distinction between “ego identity” and “craving/hating”?Are there many differences between Taoism and Buddhism?Loss of “faith” in buddhismSimilarity between creation in Abrahamic religions and beginning of life in Earth mentioned Agganna Sutta?Are there studies about the difference between meditating in the morning versus in the evening?Can one follow Hinduism and Buddhism at the same time?Are there any prohibitions on participating in other religion's practices?Psychology of 'flow'

                                fallocate: fallocate failed: Text file busy in Ubuntu 17.04? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)defragmenting and increasing performance of old lubuntu system with swap partitionIssue with increasing the root partition from the swapthis /usr/bin/dpkg returned error || ubuntu-16.04, 64bitDefault 17.04 swap file locationHow to Resize Ubuntu 17.04 Zesty Swap file size?Ubuntu freezes from online formsMy Laptop is not starting after upgrade ubuntu 16.04 (Kernel 4.8.0-38 to 04.10.0-36)hcp: ERROR: FALLOCATE FAILED!Not sure my swap is being usedWine 3.0 asking for more virtual free swap

                                Where else does the Shulchan Aruch quote an authority by name?Parashat Metzora+HagadolPesach/PassoverShulchan Aruch UTF-8Anonymous glosses in the Shulchan AruchWhy is the Shulchan Aruch definitive?Siman 32, Kitzur Shulchan Aruch: UntranslatedLitvaks/Yeshivish and Shulchan AruchBuying a Shulchan AruchEnglish version of SHULCHAN ARUCHIs there any place where Shulchan Aruch rules with the Rosh against the Rif and Rambam?Are there practices where Sepharadim do not hold by Shulchan Aruch?5th part of the shulchan aruch