Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Replace All the Occurrences of the Pattern in a Line Using the sed Command
#1

    How to Replace All the Occurrences of the Pattern in a Line Using the sed Command
   

How to Replace All the Occurrences of the Pattern in a Line Using the sed Command


   

By default, sed only substitutes the first occurrence of a specified string in every line. It looks for the initial instance, replaces it, and moves on to the following input line.

   

To replace all the pattern occurrences in one line, add a substitute flag /g for global replacement. Here’s the sed script:

   
sed ‘s/old_string/new_string/g’ samplefile.txt
   
   

For example, to replace all occurrences that contain “eagle” with “falcon” in a line inside animals.txt, run:

   
sed ‘s/eagle/falcon/g’ animals.txt
   
   

   
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)