sed command

Table of Contents


Introduction

sed is a Unix stream editor1 that performs basic text transformations on an input stream (a file or input from a pipeline). It can perform operations such as search and replace, insertion and deletion, and more, on individual lines of a file or a range of lines specified in the input. The results are then written to the output stream, which can be redirected to a file, to another command in a pipeline, or to the terminal.

The general syntax for sed command:

sed [options] 'command' input-file
  • options: Optional flags that modify the behavior of the sed command. Some common options include -n for suppressing default output, -e for executing multiple commands, and -i for performing in-place editing of files.
  • command: A single or a set of commands that specify the transformations to be applied to the input stream. Commands must be enclosed in single quotes ('') to prevent shell expansion.
  • input-file: The input file or files to be processed by sed. If no file is specified, sed will read from the standard input stream.

For example, the following command will replace all occurrences of the word “apple” with “orange” in the file fruits.txt:

sed 's/apple/orange/g' fruits.txt

Several commands that I used

  • To edit file in place use option -i with sed.

  • Backup and edit input file in-place use option -i.bkp with sed.

  • Replace a string in multiple files

    sed -i 's/foo/bar/g' *.dat
    

    This will replace word “foo” to “bar” in all *.dat files present in PWD.

    sed -i 'Ns/foo/bar/g' *.dat
    

    Here N is the Nth line of the text file. This command will find “foo” in Nth line of text fine and replace with “bar”. This is not going to check any other line except Nth line.

    To use variables in the sed command use double quotes

    sed -i "s/$var1/ZZ/g" "$file"
    

    Reference: https://askubuntu.com/a/76842

  • Replace several character from a string

    echo ramkrishna | sed 's/ram//g'
    

    or if want to replace several character from string, then

    echo "ram> krishna <Shar,ma" | sed 's/[<>,]//g'
    

    The above command will remove > or < or , from the string.

  • Show first line number 1-20 in a text file

    sed -n 1,10p FileName.txt
    
  • Append a line after text match

    sed '/<search-pattern>/a <Text-To-Append>' FileName.dat
    
  • Append a line before text match

    sed '/<search-pattern>/i <Text-To-Append>' FileName.dat
    

    Example:

    $ cat test
    
    foo
    bar
    option
    baz
    
    sed '/option/a insert text here' test
    

    Output

    foo
    bar
    option
    insert text here
    baz
    

    Ref: https://unix.stackexchange.com/questions/121161/how-to-insert-text-after-a-certain-string-in-a-file

  • Remove line of text from multiple files

    sed '/line of text/d' *.txt
    

    If you want to edit in place use option -i (edit in place). Or if you want to keep backup of old file then use -i.bak flag. If not simply put it in a bash loop:

    for file in $(ls *.txt)
    do
        sed '/line of text/d' $file > $file.new_file.txt
    done
    

    Ref: https://stackoverflow.com/questions/1182756/remove-line-of-text-from-multiple-files-in-linux

  • Copy part of file to another file

    sed -n 16,80p file1 > new_file
    
  • Add prefix/suffix to the begining of each line

    sed -i -e 's/^/prefix/' file1     # ^ stands for the begining of line
    sed -i 's/$/:80/' file.txt        # $ stands for the end of line
    
  1. A “stream editor” is a type of text editor that operates on a stream of text data, rather than on an entire file. In the case of sed, the text stream is processed one line at a time, and the transformations are applied to each line as it is read. The transformed lines are then output to another stream, which can be redirected to a file, to another command in a pipeline, or to the terminal. The stream-based approach allows sed to be used for processing large amounts of data efficiently, as it does not need to load the entire file into memory. 




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Vi-Editor
  • awk command
  • Grep command
  • GitLab workflow for CMS-AN
  • Condor Jobs
  • find command
  • EOS uses
  • Mac Settings
  • Git CheatSheet
  • ROOT CheatSheet
  • Terminal multiplexer (screen/tmux)