In this article, we will see the examples of how to remove or delete characters from a file. The syntax of sed command replacement is:

This sed command finds the pattern and replaces with another pattern. When the replace is left empty, the pattern/element found gets deleted.

Let us consider a sample file as below:

1. To remove a specific character, say ‘a’

This will remove the first occurence of ‘a’ in every line of the file. To remove all occurences of ‘a’ in every line,

2. To remove 1st character in every line:

.(dot) tries to match a single character. The  ^ tries to match a pattern(any character) in the beginning of the line.   Another way to write the same:

This tells to replace a character with nothing. Since by default, sed starts from beginning, it replaces only the 1st character since ‘g’ is not passed.

3. To remove last character of every line :

The $ tries to match a pattern in the end of the line.

4. To remove the 1st and last character of every line in the same command:

Two commands can be given together with a semi-colon separated in between.

5. To remove first character only if it is a specific character:

This removes the 1st character only if it is ‘F’.

6. To remove last character only if it is a specific character:

This removed the last character only if it s ‘x’.

7. To remove 1st 3 characters of every line:

A single dot(.) removes 1st character, 3 dots remove 1st three characters.

8. To remove 1st n characters of every line:

.{n} -> matches any character n times, and hence the above expression matches 4 characters and deletes it.

9. To remove last n characters of every line:

10. To remove everything except the 1st n characters in every line:

.* -> matches any number of characters, and the first 3 characters matched are grouped using parantheses. In the replacement, by having \1 only the group is retained, leaving out the remaining part.

11. To remove everything except the last n characters in a file:

Same as last example, except that from the end.

12. To remove multiple characters present in a file:

To delete multiple characters, [] is used by specifying the characters to be removed. This will remove all occurences of the characters a, o and e.

13. To remove a pattern  :

Not just a character, even a pattern can be removed. Here, ‘lari’ got removed from ‘Solaris’.

14. To delete only nth occurrence of a character in every line:

By default, sed performs an activity only on the 1st occurence. If n is specifed, sed performs only on the nth occurence of the pattern. The 2nd ‘u’ of ‘Ubuntu’ got deleted.

15. To delete everything in a line followed by a character:

16. To remove all digits present in every line of a file:

[0-9] stands for all characters between 0 to 9 meaning all digits, and hence all digits get removed.

17. To remove all lower case alphabets present in every line:

[a-z] represents lower case alphabets range and hence all lower-case characters get removed.

18. To remove everything other than the lower case alphabets:

^ inside square brackets negates the condition. Here, all characters except lower case alphabets get removed.

19. To remove all alpha-numeric characters present in every line:

All alpha-numeric characters get removed.

20. To remove a character irrespective of the case:

By specifying both the lower and upper case character in brackets is equivalent to removing a character irrespective of the case.

Original article here, from The UNIX School.