sed command utilities
TRANSCRIPT
SED command utilities
Table of ContentsPurpose..................................................................................................................4Sed Command Introduction...................................................................................4Regular expression................................................................................................4sed options............................................................................................................5
1. To print the file (emulates “cat”)..................................................................62. To print first 10 lines of a file (emulates “head”)..........................................63. To print the last 10 lines of a file (emulates "tail")........................................64. To print the set of lines.................................................................................65. To print only lines matches the regular expression (emulates "grep").........66. To print only lines starts with given “regular expression”............................67. To print only lines ends with given “regular expression”..............................68. To print only lines which contains exact “regular expression”......................79. To print only lines which do not matches the regular expression.................710. To print the line immediately after a regular expression...........................711. To print the section of lines between two given strings.............................712. To print all of file EXCEPT section between 2 regular expressions.............713. To print all only the line number of the regular expression.......................714. Beginning 2nd line, print every 3rd line........................................................715. Delete the first line and print the same in screen......................................716. Delete the last line and print the same in the screen................................817. Delete the set of lines from a file and print the same in screen................818. Delete leading white space (spaces, tab)..................................................819. Delete trailing white space (spaces, tab)...................................................820. Delete both leading and trailing white spaces...........................................821. Delete the comments from a file (#) and print the same in screen...........822. Delete the blank lines................................................................................823. Create a new file removing the blank lines................................................824. Delete the lines which contain particular string.........................................925. Delete the particular string in a line and print the rest..............................926. Delete all leading blank lines at the beginning of the file..........................927. Delete all the trailing blank lines at the end of the file..............................928. Delete every 3rd line in a given file............................................................929. Delete consecutive duplicate lines in a file................................................930. Insert 2 blank spaces at the beginning of each line...................................931. Insert 2 blank spaces at the end of each line............................................932. Replace the first instance of string............................................................933. Replace the last instance of a string........................................................1034. Replace the string from particular block..................................................1035. Replace all the instance of string............................................................1036. Replace all the instance of string when certain string occurs..................1037. Replace all the instance of string when certain string not occurs............1038. Replace the set of patterns to single string.............................................1039. Reverse the order of lines in a given file.................................................1040. Reverse each character on the line.........................................................1041. Reverse the file.......................................................................................1042. Add commas to numeric strings..............................................................1143. Joint two lines, previous line ending with “/”...........................................1144. Upper to Lower........................................................................................1145. Lower to Upper........................................................................................1146. Word Count.............................................................................................11
SED command utilities
47. File Copy..................................................................................................1148. Merge two files........................................................................................1149. Double space a file..................................................................................1250. Displaying File with Line Number............................................................12
SED command utilities
Purpose
This document contains some useful features of “sed” command. These commands are not very common otherwise but are very helpful in case of the need, dealing with different situations.
This document will definitely be helpful for those who work on UNIX Platform. This document can be a part of HELP document while training on UNIX for ALL specially for the new joinees in an organization.
Sed Command Introduction
“sed” is a powerful UNIX command. One can modify text across multiple files and make changes to files without opening them in an interactive editor.
sed is not suitable as a general purpose editor, and is best used to apply a set of modifications to text, particularly if you're managing more than one file. If you want to run a series of once-only edits, you are better off directly editing a file using vi or Emacs, as using sed will take much longer. But if you need to make systematic changes across multiple files, then sed is your best bet.
The sed utility is a stream editor that reads one or more text files, makes editing changes according to a script of editing commands, and writes the results to standard output. The script is obtained from either the script operand string or a combination of the option-arguments from the -e script and -f script_file options.
The sed utility is a stream editor that reads one or more text files, makes editing changes according to a script of editing commands, and writes the results to standard output. The script is obtained from either the script operand string or a combination of the option-arguments from the -e script and -f script_file options.
Sed is extremely powerful, and you can do things in sed that you can't do in any standard word processor. And because sed is external to the word processor and comes with every Unix system in the world, once you learn sed you'll have a very handy tool in your toolkit, even if you rarely use Unix.
Regular expression
We can use regular expressions to express patterns that we may find in the text. If you've ever used the '*' character on the shell command line, you've used something that's similar, but not identical to, regular expressions. Here are the special characters that you can use in regular expressions:
SED command utilities
Character Description^ Matches the beginning of the line$ Matches the end of the line. Matches any single character* Will match zero or more occurrences of the previous character[ ] Matches all the characters inside the [ ]
Few examples
Regularexpression Description/./ Will match any line that contains at least one character/../ Will match any line that contains at least two characters/^#/ Will match any line that begins with a '#'/^$/ Will match all blank lines/}^/ Will match any lines that ends with '}' (no spaces)/} *^/ Will match any line ending with '}' followed by zero or more spaces/[abc]/ Will match any line that contains a lowercase 'a', 'b', or 'c'/^[abc]/ Will match any line that begins with an 'a', 'b', or 'c'
sed options
The default behavior of sed outputs all lines, even if no changes are made. The -n option will skip lines that aren't modified.
SED command utilities
1. To print the file (emulates “cat”)
This command can be used to print the first 10 lines of given file
sed ':' FILENAME(or) sed -n 'p' FILENAME
2. To print first 10 lines of a file (emulates “head”)
This command can be used to print the first 10 lines of given file
sed 10q FILENAME | more
3. To print the last 10 lines of a file (emulates "tail")
This command can be used to print the last10 lines of given file
sed -e :a -e '$q;N;11,$D;ba' FILENAME | more
4. To print the set of lines
This command can be used to print 5 lines starting line number 45 to line number 50.
sed ‘45,50p’ FILENAME | more
5. To print only lines matches the regular expression (emulates "grep")
This command can be used to print the lines which contain the given regular expression.
sed -n '/regexp/p' FILENAME
6. To print only lines starts with given “regular expression”
This command can be used to print the lines which contain the given regular expression at the beginning of a line.
sed -n '/^regexp/p' FILENAME
7. To print only lines ends with given “regular expression”
This command can be used to print the lines which contain the given regular expression at the end of a line
sed -n '/regexp$/p' FILENAME
SED command utilities
8. To print only lines which contains exact “regular expression”
This command can be used to print the lines which contain only the given regular expression.
sed -n '/^regexp$/p' FILENAME
9. To print only lines which do not matches the regular expression
This command can be used to print the lines which contain the given regular expression.
sed -n '/regexp/!p' FILENAME
10. To print the line immediately after a regular expression
This command can be used to print the line immediate after which contain the given regular expression.
sed -n '/regexp/{n;p;}' FILENAME
11. To print the section of lines between two given strings
This command can be used to print the lines between two given strings.
sed '/STRING1/,/STRING2/p' FILENAME
12. To print all of file EXCEPT section between 2 regular expressions
This command can be used to print all of file except section between 2 strings.
sed ‘/STRING1/,/STRING2/d' FILENAME
13. To print all only the line number of the regular expression
This command can be used to print the line number of the given regular expression
sed -n '/regexp/=' FILENAME
14. Beginning 2nd line, print every 3rd line
This command can be used to print every 3rd line starting from 2nd line
sed -n '2,${p;n;n;}' FILENAME
15. Delete the first line and print the same in screen
This command can be used to delete the first line from the file and print the same in the screen
sed -e '1d' FILENAME |more
SED command utilities
16. Delete the last line and print the same in the screen.
This command can be used to delete the last line from the file and print the same in the screen
sed '$d' FILENAME |more
17. Delete the set of lines from a file and print the same in screen
This command can be used to delete the set of given range from the file and print the same in the screen
sed -e '2,10d' FILENAME |more
18. Delete leading white space (spaces, tab)
This command can be used to delete the leading blank spaces for all the lines.
sed 's/^[ \t]*//' FILENAME |more
19. Delete trailing white space (spaces, tab)
This command can be used to delete the trailing blank spaces for all the lines.
sed 's/[ \t]*$//' FILENAME|more
20. Delete both leading and trailing white spaces
This command can be used to delete the trailing and leading blank spaces for all the lines.
sed 's/[ \t]*$//' FILENAME|m
21. Delete the comments from a file (#) and print the same in screen
This command can be used to delete the UNIX comments in a given file and print the same in the screen.
sed -e '/^#/d' FILENAME |more
22. Delete the blank lines
This command can be used to delete the blank lines in a given file
sed '/^$/d' FILENAME |more(or) sed -n '/./p' FILENAME | more
23. Create a new file removing the blank lines
This command can be used to delete the blank lines in a given file
sed -ne '/^$/d; w NEW-FILE' OLD-FILE
SED command utilities
24. Delete the lines which contain particular string
This command can be used to delete the lines which contain a particular string.
sed -e '/STRING/d' FILENAME |more
25. Delete the particular string in a line and print the rest
This command can be used to delete the particular string in a given file.
sed -e 's/STRING//' FILENAME |more
26. Delete all leading blank lines at the beginning of the file
This command can be used to delete all the leading blank lines at the beginning of the file.
sed '/./,$!d' FILENAME
27. Delete all the trailing blank lines at the end of the file
This command can be used to delete all the trailing blank lines at the end of the file.
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' FILENAME
28. Delete every 3rd line in a given file
This command can be used to delete every third line in a given file.
sed 'n;n;d' FILENAME
29. Delete consecutive duplicate lines in a file.
This command can be used to delete the consecutive duplicate line and keeps the first one.
sed '$!N; /^\(.*\)\n\1$/!P; D' FILENAME
30. Insert 2 blank spaces at the beginning of each line
This command can be used to insert 2 blank spaces at the beginning of each line.
sed ‘s/^/ /’ FILENAME
31. Insert 2 blank spaces at the end of each line
This command can be used to insert 2 blank spaces at the end of each line.
sed ‘s/$/ /’ FILENAME
32. Replace the first instance of string
This command can be used to replace the first occurrence of given string/
SED command utilities
sed ‘s/STRING1/STRING2/’ FILENAME
33. Replace the last instance of a string
This command can be used to replace the last occurrence of a given string
sed 's/\(.*\)STRING1/\STRING2/' FILENAME
34. Replace the string from particular block
This command can be used to replace the string in a given block.
sed ‘2,4 s /STRING1/STRING2/' FILENAME
35. Replace all the instance of string
This command can be used to replace the first occurrence of given string
sed ‘s/STRING1/STRING2/g’ FILENAME
36. Replace all the instance of string when certain string occurs
This command can be used to replace the first occurrence of given string/
sed ‘/SEARCH_STRING/s/STRING1/STRING2/g’ FILENAME
37. Replace all the instance of string when certain string not occurs
This command can be used to replace the first occurrence of given string
sed ‘/SEARCH_STRING/!s/STRING1/STRING2/g’ FILENAME
38. Replace the set of patterns to single string
This command can be used to replace the first occurrence of given string
sed ‘s/STRING1\|STRING2\|STRING3/STRING/g’ FILENAME
39. Reverse the order of lines in a given file
This command can be used to reverse the order of lines in a given file.
sed '1!G;h;$!d' FILENAME(or) sed -nf '1!G;h;$p' FILENAME
40. Reverse each character on the line
This command can be used to reverse each character on the line.
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' FILENAME
41. Reverse the file
This command can be used to reverse the contents of the file
SED command utilities
sed '1!G;h;$!d' FILENAME | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
42. Add commas to numeric strings
This command can be used to add commas to numeric string, changing "1234567" to "1,234,567"
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' FILENAME
43. Joint two lines, previous line ending with “/”
This command can be used to joint two lines, where previous line ending with “\”
sed -e :a -e '/\\$/N; s/\\\n//; ta' FILENAME
44. Upper to Lower
This command can be used to convert a file from UPPER case to lower case
sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' FILENAME
45. Lower to Upper
This command can be used to convert a file from lower case to upper case
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' FILENAME
46. Word Count
This command can be used to count the lines in a file
sed -n '$=' FILENAME
47. File Copy
This command can be used to copy the file.
sed -n 'w NEW-FILE' OLD-FILE
48. Merge two files
This command can be used to merge the contents of two files
sed 'r FILE1' FILE2 | sed -n 'w NEW-FILE'
SED command utilities
49. Double space a file
This command can be used to double space the given file
sed 'G; w NEW-FILE' OLD-FILE
50. Displaying File with Line Number
This command can be used to print the file with line number.
sed = filename | sed 'N;s/\n/ : /'