Skip to main content
Engineering LibreTexts

05-D.7.6: Handling Text Files - sed Command

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The sed Command

    The sed command is a stream editor which is used to perform basic text transformations on an input stream, usually either a file or input from a Linux command pipeline. However the most common use of sed command is for substitution or for find and replace. By using sed you can edit files even without opening them, which is a much quicker way to find and replace something in a file than first opening that file in VI Editor and then changing it.

    • sed is a powerful text stream editor. It can do insertion, deletion, and search and replace (substitution).
    • sed command in Linux supports regular expression which allows it to perform complex pattern matching.


    sed [ OPTIONS ] [SCRIPT] [INPUTFILE...] 

    Command Options:

    Options Option Meaning
    -n, --quiet, --silent Suppress automatic printing of pattern space.
    -e script, --expression=script Add the script to the commands to be executed.
    -f script-file, --file=script-file Add the contents of script-file to the commands to be executed.
    --follow-symlinks Follow symlinks when processing in place.
    -i[SUFFIX], --in-place[=SUFFIX] Edit files in place (this makes a backup with file extension SUFFIX, if SUFFIX is supplied).
    -l N, --line-length=N Specify the desired line-wrap length, N, for the "l" command.
    --POSIX Disable all GNU extensions.
    -r, --regexp-extended Use extended regular expressions in the script.
    -s, --separate Consider files as separate rather than as a single continuous long stream.
    -u, --unbuffered Load minimal amounts of data from the input files and flush the output buffers more often.
    --help Display a help message, and exit.
    --version Output version information, and exit.

    A brief example: notice we have the work 'geek' in the file colossus.txt. This word should be Greek. We can easily fix that with a single command.

    pbmac@pbmac-server $ grep geek colossus.txt 
    Not like the brazen giant of geek fame,
    pbmac@pbmac-server $ sed 's/geek/Greek/' colossus.txt 
    Not like the brazen giant of Greek fame,
    With conquering limbs astride from land to land;
    Here at our sea-washed, sunset gates shall stand
    A mighty woman with a torch, whose flame
    Is the imprisoned lightning, and her name
    Mother of Exiles. From her beacon-hand
    Glows world-wide welcome; her mild eyes command
    The air-bridged harbor that twin cities frame.

    The sed command can be broken down this way: s says to search; the word geek between the slash characters - /geek/ - is the search term; the second term - /Greek/ - is the replacement term; and then we provide the file name. So - we search for "geek" and replace it with "Greek." The file remains unchanged though, and you would have to save the changes.

    Adapted from:
    "Sed Command in Linux/Unix with examples" by Akshay Rajput and Mohak Agrawal., Geeks for Geeks is licensed under BY-SA 4.0

    05-D.7.6: Handling Text Files - sed Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?