What is P in the SED command


The shell commandsed (stream editor) is a line editor and, unlike text editors like vi, does not work interactively. Instead, it is determined beforehand on the command line which text areas are to be deleted, changed or inserted. sed processes every line with the listed commands and writes them to a buffer, the content of which - unless otherwise defined - appears on the standard output (stdout).

The command N reads another line into the "pattern space" without removing the current line. Other commands terminate sed before the entire source file has been processed. In addition to the "pattern space", there is also the "hold space".


Sed normally receives the texts to be processed via the standard input (stdin). One or more files can be specified. To prevent the shell from evaluating the command, it is enclosed in single quotation marks.

sed [options] 'commands' files

The specification of which lines or file areas are to be processed is known as the "address".

The commands can also be written to a script file.

sed -f scriptfile sourcefile> targetfile

Signs to be aligned

sed uses basic regular expressions - to treat the characters as ordinary characters, they must be backed up with the backslash. Letters, numbers and the characters do not have to be aligned.

In reality, all of this is much more complicated ... see


Output of the corresponding lines on the screen.
Delete the specified area.
Replacement of the specified string.
Insertion of text after the addressed lines (when used in scripts).
Insertion of text in front of the addressed lines (when used in scripts).
Replace lines or ranges of lines (when used in scripts).
Reading the content of a file and placing the content behind the corresponding address.
Writing the addressed lines or areas to a new file.
changes all "CA" of the source file to "California". Since no address is given, all lines are processed.
replaces one or more spaces in a row with a single space.
changes all "CA" of the source file to "California" if the respective line contains "Sebastopol". Since there is an address here (Sebastopol), only the lines corresponding to the address are processed.
deletes all lines
  • deletes the first line
  • deletes the last line
  • deletes blank lines
  • deletes all lines on which the first pattern fits up to and including the first line on which the second pattern fits
deletes blank lines only in the area specified by the two patterns. Several commands can appear between the curly brackets (each of which must be on its own line as in the example).


Option "p"

Output of the content of the file "file".

$ sed '' file

Output of the contents of the file "file", whereby the third line is displayed twice.

$ sed '3p' file

Output only the third line of the file "file".

$ sed -n '3p' file

Option "d"

Delete the first line of the file "file".

$ sed '1d' file

Delete the second to fourth lines of the "file" file.

$ sed '2,4d' file

Delete every third line (starting from the first) of the file "file".

$ sed '1 ~ 3d' file

Delete line 5 and all subsequent lines up to the end of the file "file".

$ sed '5, $ d' file

Deletion of all lines that begin with a hash sign (e.g. for a better view of configuration files). However, the blank lines are also output.

$ sed '/ ^ # / d' / etc / hosts

Output of all lines that begin with a character that is not a hash sign. Empty lines are therefore not output.

$ sed -n '/ ^ [^ #]. * / p' / etc / hosts

Option "s"

The "s" option replaces character strings. The command is followed by a separator, the search pattern, another separator, the new character string to be inserted and a final separator. Any character can be used as a separator, but it must not appear in the search pattern.

Replace the first character string "abc" with "xyz" in each line of the file "file".

$ sed 's / abc / xyz /' file

Replace all "abc" strings with "xyz" on each line of the "file" file.

$ sed 's / abc / xyz / g' file

Replace all strings "abc" with "xyz" in line 3 of the file "file".

$ sed '3s / abc / xyz / g' file

Replace all strings "http://abc.org/" with "http://xyz.org/" on each line of the file "file". Since the slash occurs here in the search pattern, the hash symbol is used as a separator.

$ sed 's # http: //abc.org/#http: //xyz.org/#g' file

Replace line breaks ("\ n") in file "file" with the string "ABCD".

$ sed ': M; N; $! bM; s # \ n # ABCD # g' file

Converts the first letter of each line of the file "file" to an uppercase letter.

$ sed 's / ^ \ (\ <. \) / \ U \ 1 / g' file

Converts the first letter of each line in the file "file" to lower case.

$ sed 's / ^ \ (\ <. \) / \ L \ 1 / g' file

Conversion of the first letter after the first parenthesis "(" of each line into a capital letter.

$ echo "abc (Zuz) hjh" | sed 's / (\ (. \) / (\ U \ 1 /' abc (zu) hjh

Multiple commands

Delete line 5 and all subsequent lines up to the end of the file "file". In the rest of the file, the string "KDE" is replaced by "Gnome".

$ sed -e '5, $ d' -e 's / KDE / Gnome / g' file or $ sed '{5, $ d;' s / KDE / Gnome / g 'file

Read and write

Insert the "infile" file after the third line of the "file" file.

$ sed '3r infile' file

Write lines 25 to 35 of the file "file" to the new file "outfile".

$ sed '25, 35w outfile 'file

sed script

Several sed commands can also be written in a script file, which is then assigned to the desired files with the "-f" option.

Delete line 2 and add the text "After the third line" after line 3 of the file "file". For the command "a" it is important that a backslash and a line break are inserted after the command ("3a"). The text to be inserted is then on a new line. If there are several lines, each (except the last) must be closed with a backslash.

$ more scriptfile 2d 3a \ After the third line. \ One more thing. \ And that's it. $ sed -f scriptfile file

Write to a file

sed does not change the original file by default, but writes its output to the standard output (stdout).

Write the output to the "outfile" file.

$ sed -f scriptfile file> outfile

Write the output directly to the original "file".

$ sed -i -f scriptfile file

Writing the output directly into the original file "file", whereby the original file remains under the name "file.bak".

$ sed -i .bak -f scriptfile file

Insert the character string "abc" including line break (LF) at the very beginning of the "file.txt" file so that it appears as the first line.

$ sed -i '1s / ^ / abc \ n /' file.txt

Combination with other programs

Suppose there are several files in a directory with spaces and hyphens in their names, which you want to replace with underscores. Such a script should be tested first. If everything is OK, the files can be renamed using the mv command.

$ ls -1 * .txt test 1-0.txt test 2-5.txt $ ls -1 * .txt | sed 's / [-] / _ /' test_1-0.txt test_2-5.txt $ for i in * .txt; do mv -v "$ i" $ (echo $ i | sed 's / [-] / _ /'); done "test 1-0.txt" -> "test_1-0.txt" "test 2-5.txt" -> "test_2-5.txt"

Count all words in the file "file" that contain duplicate letters. "(. \)" is the intermediate buffer, "\ 1" the buffer referencing.

$ sed -n '/\(.\)\1/p' file | toilet -l

Replace the first character ":" of the file "file" with a space.

$ sed 's /: / /' file

Replace all characters ":" of the file "file" with a space.

$ sed 's /: / / g' file

Replace all characters ":" of the file "file" with the character string space colon space (":"). The ":" character is loaded into a buffer and referenced with "\ 1".

$ sed 's / \ (: \) / \ 1 / g' file

Replace the separator ":" in the file "/ etc / passwd" with a space and output to the screen. There are two ways to do this.

$ sed -e "s,:,, g" / etc / passwd $ cat / etc / passwd | sed -e "s,:,, g"

Output of the first five lines of the file "file.txt".

$ sed '5q' file.txt

Output of the file "file.txt" from line 5 to the end of the file.

$ sed -n '5, $ p' file.txt

Output of lines 10 up to and including 20 of the file "file.txt".

$ sed -n '10, 20p 'file.txt

Output of the file "file.txt", whereby the fourth and fifth lines are deleted.

$ sed '4,5d' file.txt

Replace the IP address "" in all files in the "/ etc" directory (e.g. if a machine has just been cloned) with the address "".

$ for i in $ (grep -rlIs "" / etc); do echo "changing file $ i"; \ sed 's / / /' $ i> sed.tmp; mv sed.tmp $ i; done

Insert a line above a specific search pattern.

$ echo "This is a text \ nwith a SEARCH PATTERN" | \ sed -e '/ SEARCH PATTERN / {; i \' -e 'which could also be in a file' -e '}' which could also be in a file This is a text \ nwith a SEARCH PATTERN

Comment out all lines beginning with lower case letters in the file "file.txt" with the "#" character.

$ sed "/ ^ [a-z] / s / ^ / # /" file.txt


A file "file" is given with the following content.

361 405

The numbers contained in the file are used in the following command instead of the placeholder "\ 1".

$ sed 's /\(.*\)/ 0000: \ 1 \ n9998: \ 1 \ n9999: \ n /' file> newfile $ more newfile 0000:361 9998:361 9999: 0000:405 9998:405 9999:

Web links