Sed Cheat Sheet



  1. Sed Cheat Sheet Pdf
  2. Sed Command Cheat Sheet
  3. Ubuntu Sed Cheat Sheet

Sed and Awk 101 Hacks – Enhance your UNIX / Linux Life with Sed and Awk. Awk Cheat Sheet www.thegeekstuff.com 0 Leading zeros. Add a 0 (zero) before the number. '%05s' Awk Functions index(str1,str2) Give location of str2 in str1 length(str1) Length of the string.

Sed Cheat Sheet Sed command line options sed options sed-command input-file Option Description Example-n: Suppress default pattern space printing. Sed Cheat Sheet www.thegeekstuff.com Sed command line options Sed syntax: sed options sed-command input-file-n Suppress default pattern space printing sed -n '3 p' employee.txt -i Backup and modify input file directly sed -ibak 's/John/Johnny/' employee.txt -f Execute sed script file sed -f script.sed employee.txt. Name Command; Insert string to the begining of lines: sed -i 's/^/head /g' my-file: Insert string to the end of lines: sed -i 's/$/ tail/g' my-file: Add content after nth line.

Sed

Introduction to the sed Editor

The term sed stands for stream editor. Sed can take its input from standard in, apply the requested edits on the stream, and automatically put the results to standard out. The sed syntax allows for an input file to be specified on the command line.

You do not need to interact with the sed editor while it is running; therefore, it has also been termed a batch editor. This is in contrast to such editors as vi and ed, which are interactive. Because sed does not require interaction, you can place sed commands in a script. You can call the script file and run it against the data file to perform repetitive editing operations.

Command Format

The following shows the syntax for the sed command:

The sed editor is capable of performing text-pattern substitutions and text-pattern deletions using regular expression syntax. These are the same regular expression characters used by grep.

The sed command offers capabilities that are an extension of interactive text editing. If you need to search and replace text strings in a large number of files, sed is most useful.

Editing Commands

Sheet

The sed editor uses a editing commands (shown in Table) that are similar to those you would use for vi and ed.

CommandFunction
dDeletes line(s)
pPrints line(s)
rReads a file
sSubstitutes one string for another
wWrites to a file

The sed command has two options:

Option Function
-nSuppresses the default output
-fReads sed commands from a script file

Sample data used in the examples

The following examples execute sed commands against the file named “data.txt”. The content of the file data.txt is as follows.

Using sed to Print Text

Example 1 : Printing range of lines

The following example shows the use of the p (print) command, which prints a range of lines to stdout. The range is specified by a starting address followed by a comma and then the ending address. The default output of sed is each line that it reads. To suppress the default output, use the -n option.

Example 2 : Printing lines containing a specific pattern

The following command prints all lines with the pattern west in it. Use the forward slash (/) to delimit the regular expression.

Example 3 : Printing line with multiple patterns

The following command prints the first line containing the pattern west, up to and including the next line containing the pattern southern.

Example 4 : Printing line with pattern and all lines after that

Sed Cheat Sheet

The following command prints the first line containing the pattern Chris, up through the last line of the file.

Example 5 : Functionality similar to grep

The pattern might contain the regular expression characters used by grep. The following example prints all lines that begin with an s and end with a 5.

Using sed to Substitute Text

CommandExampleEditing Action
ssed s/x/y/option Search and replace. The search pattern x is replaced with pattern y. The search and the replacement pattern are regular expressions in most cases, and the search and replace behavior can be influenced through various options.
ysed y/abc/xyz/ Replace every character from the set of source characters with the character that has the same position in the set of destination characters.

Example 1 : Search and substitute a specific pattern

The sed s command allows for a search and substitution operation to occur on the text. The command uses a pattern search and a literal string replacement. The replacement string characters are taken literally without metacharacter expansion.

The sed command checks each line of the file and substitutes the first occurrence of the old string with the new string. Subsequent occurrences of the old string within the same line are left unchanged.

Example 2 : Search and substitute a specific pattern globally

The following example shows the g (global) command with the s (search and substitute) command, and it replaces all occurrences of the old string with the new string.

Example 3 : Search and substitute with search pattern included in the substitution

Occasionally with a search and substitute, the old string will be part of the new replacement string, which you can accomplish by placing a & (ampersand) in the replacement string. The location of the & determines the location of the old string in the replacement string.

The objective of the following examples is to write a command that searches for all lines that end with a single digit in the last field and replace the single digit with the single-digit number plus the string Single Digit.

To properly identify the lines with single-digit numbers in the last field, consider the following sed command. Tabs separate the fields with each line.

Sed Cheat Sheet Pdf

The following command searches for all lines that end with a single digit in the last field and replaces the single digit with the single-digit number plus the string Single Digit.

Using sed to Delete Text

Example 1 : Delete a range of lines

The following command deletes Lines 4 through 8 from the output.

Sed Command Cheat Sheet

Example 2 : Deleting lines containing a specific pattern

The following command deletes any line containing the pattern west.

Example 3

The following command deletes any line beginning with the pattern west.

Example 4

The following command deletes the range of lines beginning with the first line containing the pattern south, up through the next line of the file containing north.

Additional sed Functionality – Editing Commands

Additional sed editor commands are shown in the following table.

CommandFunction
aAppends text
cChanges text in the current line with new text
iInserts text above current line

Placing Multiple Edits in a Single sed Command

Sed github

There are times when you want to perform several edits on the file. Rather than using multiple sed commands, you can use the -e option to place the edits in the same command line. The edits are performed in the order that you specify.

Ubuntu Sed Cheat Sheet

Example 1

Delete lines carefully. You might perform edits on a line of text and subsequently remove that line of text from the output. This occurs in the following example with Line 1 of the input file.