Try it here
Subscribe
Sed Command

Linux/Unix Sed Command

linux/unix_sed_command

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion.

The most common use of SED command in UNIX is for substitution or for find and replace.By using SED you can edit files even without opening it, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.
  • SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
  • SED command in unix supports regular expression which allows it perform complex pattern matching.

Syntax :

sed OPTIONS... [SCRIPT] [INPUTFILE...] 
  1. Viewing a file

    • Range of lines of a file

      The following sed command will return line from 2 to 12 from myfile.txt

      # sed -n '2,12p' myfile.txt
    • Entire file except a given range

      Exclude lines 15 through 30 from myfile.txt.

      # sed '15,30d' myfile.txt
    • Non-consecutive lines and ranges

      Example for lines 5-10 and 15-20 from myfile.txt:

      # sed -n -e '5,10p' -e '15,20p' myfile.txt

      The -e option allows us to execute a given action (in this case, print lines) for each range.

  2. Replacing or substituting string

    • Replacing all the occurrence of the pattern in a line

      The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

      Replace every instance of the word story with JIRA in myfile.txt :

      # sed 's/story/JIRA/g' myfile.txt

      Note −> To ignore character case use gi

      # sed 's/story/JIRA/gi' myfile.txt
    • Replacing the nth occurrence of a pattern in a line

      Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "story" with "JIRA" in a line.

      # sed 's/story/JIRA/2' myfile.txt
    • Replacing from nth occurrence to all occurrences in a line

      Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth… "story" word with "JIRA" word in a line.

      # sed 's/story/JIRA/3g' myfile.txt
    • Replacing string on a specific line number

      You can restrict the sed command to replace the string on a specific line number. An example to replace the string only on the fifth line.

      # sed '5 s/story/JIRA/' myfile.txt
    • Duplicating the replaced line with /p flag

      The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

      # sed 's/story/JIRA/p' myfile.txt
    • Printing only the replaced lines

      Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

      # sed -n 's/story/JIRA/p' myfile.txt
    • Replacing string on a range of lines

      You can specify a range of line numbers to the sed command for replacing a string.

      Below example replaces story with JIRA only within a line range 3 through 5.

      sed '3,5 s/story/JIRA/g' myfile.txt

      Below is the example to replace story with JIRA from line 2 to the last line in the file

      # sed '2,$ s/story/JIRA/g' myfile.txt

      Here $ indicates last line in the file.

    • Replacing words only if a separate match is found

      Sometimes replacing all instances of a given word, or a random few, is not precisely what we need. Perhaps we need to perform the replacement if a separate match is found.

      For example, we may want to replace start with stop only if the word services is found in the same line. In that scenario, here’s what will happen:

      We need to start partying at work,
      but let’s remember to start all services first.

      In the first line, start will not be replaced with stop since the word services does not appear in that line, as opposed to the second line.

      # sed '/services/ s/start/stop/g' myfile.txt
    • Performing two or more substitutions at once

      You can combine two or more substitutions one single sed command. Let’s replace the words story with JIRA and bug with hotfix.

      This can be done by using an ordinary sed substitution command followed by a semicolon and a second substitution command:

      # sed -i 's/story/JIRA/gi;s/bug/hotfix/gi' myfile.txt
  3. Deleting line from file

    • Delete 10th line from a file

      # sed '10d' myfile.txt
    • Delete a last line

      # sed '$d' myfile.txt
    • Delete line from range 2 to 8

      # sed '2,8d' myfile.txt
    • Delete from 5th to last line

      # sed '5,$d' myfile.txt
    • Delete pattern matching line

      # sed 'xyz/d' myfile.txt

Writer profile pic

Uk01 on Feb 19, 2015 at 12:02 am


If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.