Linux sed command with example

The sed command is a editor which is used for filtering and transforming data. It is commonly used in Linux and Unix OS. Here are some examples of using sed command in Linux with different options and commands:

  1. Substituting a pattern with a replacement string:
sed 's/old/new/g' file.txt

This command replaces all occurrences of "old" with "new" in the file file.txt.

  1. Replacing the first occurrence of a pattern with a replacement string:
sed 's/old/new/' file.txt

This command replaces the first occurrence of "old" with "new" in the file file.txt.

  1. Deleting a line containing a pattern:
sed '/pattern/d' file.txt

This command deletes all lines containing the pattern "pattern" in the file file.txt.

  1. Printing only the lines containing a pattern:
sed -n '/pattern/p' file.txt

This command prints only the lines containing the pattern "pattern" in the file file.txt.

  1. Using a sed script file:
sed -f script.sed file.txt

This command executes the sed commands in the file script.sed on the file file.txt.

  1. Using extended regular expressions:
sed -r 's/([a-z]+)\s([0-9]+)/\2 \1/' file.txt

This command uses extended regular expressions to swap the order of the first word and the first number in each line of the file file.txt.

  1. Modifying a file in place:
sed -i 's/old/new/g' file.txt

This command modifies the file file.txt in place by replacing all occurrences of "old" with "new".

  1. Translating characters from one set to another:
sed 'y/abcdef/ABCDEF/' file.txt

This command replaces "a" with "A", "b" with "B", "c" with "C", and so on in the file file.txt.

  1. Appending text to the end of a line:
sed 's/$/ new text/' file.txt

This command appends " new text" to the end of each line in the file file.txt.

  1. Inserting text at the beginning of a line:
sed 's/^/new text /' file.txt

This command inserts "new text " at the beginning of each line in the file file.txt.

  1. Removing leading whitespace:
sed 's/^[ \t]*//' file.txt

This command removes leading whitespace (spaces and tabs) from each line in the file file.txt.

  1. Removing trailing whitespace:
sed 's/[ \t]*$//' file.txt

This command removes trailing whitespace (spaces and tabs) from each line in the file file.txt.

  1. Extracting a specific line:
sed -n '3p' file.txt

This command extracts the third line of the file file.txt and prints it to the console.

  1. Extracting a range of lines:
sed -n '2,4p' file.txt

This command extracts lines 2 to 4 of the file file.txt and prints them to the console.

  1. Using a regular expression to match a pattern:
sed '/^#.*$/d' file.txt

This command deletes all lines in the file file.txt that begin with a "#" character (comment lines in many configuration files).

  1. Using a backreference to replace a pattern:
sed 's/\([a-z]*\)123/\11234/g' file.txt

This command replaces any occurrence of a lowercase letter followed by "123" with the letter followed by "234" in the file file.txt. The backreference \1 refers to the matched lowercase letter.

  1. Deleting multiple patterns:
sed '/pattern1/d; /pattern2/d' file.txt

This command deletes all lines containing either "pattern1" or "pattern2" in the file file.txt. Multiple sed commands can be separated by semicolons.

  1. Deleting blank lines:
sed '/^$/d' file.txt

This command deletes all blank lines in the file file.txt. The regular expression ^$ matches the beginning of the line followed by the end of the line, indicating a line with no characters.

  1. Using the hold space to swap lines:
sed '1!G;h;$!d' file.txt

This command swaps each line in the file file.txt with the previous line, effectively reversing the order of the file. The hold space is used to temporarily store the previous line.

  1. Printing the line number:
sed '=' file.txt | sed 'N;s/\n/ /'

This command adds the line number to each line in the file file.txt, separated by a space. The first sed command adds the line number to each line, and the second sed command appends the next line and replaces the newline character with a space.

Here are some of the most commonly used sed commands options:

  1. s: Substitutes the first occurrence of a pattern with a replacement string. For example, s/old/new/ replaces the first occurrence of "old" with "new".

  2. g: Replaces all occurrences of a pattern with a replacement string. For example, s/old/new/g replaces all occurrences of "old" with "new".

  3. p: Prints the current pattern space.

  4. d: Deletes the current pattern space.

  5. y: Translates characters from one set to another. For example, y/abc/def/ replaces "a" with "d", "b" with "e", and "c" with "f".

  6. q: Exits the sed script immediately.

These are just a few examples of how to use the sed command in Linux. If you have further questons then you can reach out to us on whatsapp numbers: 7838238895/8909068089/8920228066