Unix Sed Command Utility

09/12/2010

RETAIL & CPG Anto Thomas anto.thomas@tcs.com
HU UH

TCS Public

.....6 Deleting the last line in the input file................................................................................................................3 Sed Utility ................................................................................................................................Unix Sed Command Utility Contents Contents ..............................................................................................5 Deleting lines in a file using sed command .......2 Sed .............................Search and replace ...........................................4 g flag ......................................................................................................................................8 Internal Use 2 ....................................................7 To remove all occurrences of the character space using sed command .............6 Replace or substitute all occurrences of a pattern using sed command.................................................................................................................4 Forward slash .................................................6 To delete the lines 2 through 4 in the above file....................................................................................................4 Backslash ........................................................................4 s -...........7 Deleting lines which matches a pattern using sed command...........................................................................................................................................8 Deleting the last line which matches a pattern using sed command...........................................................................5 Ampersand ........................4 syntax for basic sed commands: ...............6 Delete all occurrences of a pattern in a file using sed command..........................8 Deleting the first line which matches a pattern using sed command..........5 Applications of sed command..........................................................................................An Introduction..................................... using sed command ......

Unix Sed Command Utility Sed . Internal Use 3 . They let shell programmers easily edit text files and filter the output of other commands using regular expressions. thus all the input you feed into it passes through and goes to STDOUT and it does not change the input file. As its name implies. sed is stream oriented. sed (which stands for stream editor) was created as an editor exclusively for executing scripts.An Introduction The most powerful text filtering tools in the UNIX environment are sed and awk.

you would run the following command: sed s/logfile/logfile. More simply. To use sed. separated by forward slashes (/).txt/ sed. The forward slashes are used to bind regular expressions in sed. This is important because many files.Unix Sed Command Utility Sed Utility sed is short for stream editor. For example. make extensive use of the same characters that are used to execute a regular expression. sed is commonly used in UNIX shell scripts to filter large sets of text files.The traditional. In your sed. syntax for basic sed commands: sed s/REGULAREXPRESSION/REPLACEMENTSTRING/flags INPUT_FILE Forward slash All of the special characters explained earlier for use with grep also work in sed. especially when you are writing code. For instance.Search and replace The s indicates you want to execute a search and replace. run this command: sed s/logfile/logfile. sed is also a text editor. but it is a command-line utility instead of an interactive utility.txt For future reference.txt. to send the output to sed_new.txt > sed_new. modern-day definition of a text editor is an interactive application that can be used to create and edit text files.txt. because it escapes the next character from the regular expression interpretation. A basic expression in sed is composed of four parts. s -. which makes it an extremely powerful tool for batch editing. however.txt Backslash The backslash (\) is called the escape character. if you simply want to replace the term logfile with logfile. you must learn some additional syntax.txt/ sed.txt file. putting a backslash before a special character makes the character a plain item instead of a command item. you'll notice Internal Use 4 . the output can be captured or sent into a new file.

the ampersand would hold that value. bash-3. In other words.Introducing filters and regular expressions sed s/project/project_name/g sed. sed s/[a-z]*:/new_\&/g sed.txt g flag Execute the same sed command. though mainly used for substitution.txt This time.Unix Sed Command Utility that the dollar sign is used. If you want to replace $project but not project. The text file used to illustrate the below sed commands is shown below. it can also do numerous other pattern matching tasks like searching and printing lines which matches a pattern.00# This is line 1 This is line 2 This is line 3 This is line 4 This is line 5 cat textfile. if [a-z]*: turned out to be project: on a particular line. removing lines which matches a pattern. but this time tack a g on the end.txt Internal Use 5 . both instances of project were changed to project_name on the first Ampersand The ampersand (&) represents the string that was matched by your regular expression. you need to use the escape character in your search and replace: sed s/\$project/\$project_name/ sed. deleting specific lines in the input file.txt Applications of sed command Sed command can be used to search and manipulate input text in many ways.

00# sed '2.00# sed '$d' textfile.txt This is line 2 This is line 3 This is line 4 This is line 5 Deleting the last line in the input file bash-3. bash-3. Replace or substitute all occurrences of a pattern using sed command In the above text file. using sed command bash-3.Unix Sed Command Utility Deleting lines in a file using sed command Deleting the first line in the input file bash-3.txt This is line 1 This is line 5 the 'd' flag is used to delete lines.txt This is line number 1 This is line number 2 This is line number 3 This is line number 4 Internal Use 6 . if you need to replace all occurrences of the pattern "line" with the text "line number". the following sed command can be used (the 's' flag in the sed command is used to substitute text which matches a pattern).4d' textfile.00# sed '1d' textfile.00# sed 's/line/line number/g' textfile.txt This is line 1 This is line 2 This is line 3 This is line 4 To delete the lines 2 through 4 in the above file.

Delete all occurrences of a pattern in a file using sed command This is a simple application of the /s flag where the target string is none.txt This is This is This is This is This is 1 2 3 4 5 Note that the word to be replaced upon matching the pattern is none in the above command ('/s/line//') To remove all occurrences of the character space using sed command bash-3.00# sed s/' '//g textfile.Unix Sed Command Utility This is line number 5 Note: The /g flag in the above command is used to instruct sed to substitute globally i.txt Thisisline1 Thisisline2 Thisisline3 Thisisline4 Thisisline5 Internal Use 7 .e.txt Thisisline1 Thisisline2 Thisisline3 Thisisline4 Thisisline5 Also note that if you don't use single quotes in the above sed command.00# sed 's/line//' textfile. bash-3. to remove all occurrences of word "line" in the above file.00# sed 's/ //g' textfile. the sed command would be bash-3. without /g only the first word matching the pattern in a line will be replaced. all occurrences of the input pattern in the line will be replaced and as a result all words matching the pattern in the input file will be substituted by the other. then the space pattern should be enclosed in quotes for the command to work. For example.

txt This is line 1 This is line 2 This is line 3 This is line 4 Internal Use 8 . to delete lines matching the pattern "line 1" in the above file.}' textfile. Deleting the first line which matches a pattern using sed command bash-3. this is the way to separate sed flags in command/scripts.}' textfile.txt This is line 2 This is line 3 This is line 4 This is line 5 Now using the above commands. which matches a pattern).txt This is line 2 This is line 3 This is line 4 This is line 5 In the above command.00# sed '/line 1/d' textfile. for example. one can also delete the first and last line in a file which matches a pattern. the command would be bash-3.Unix Sed Command Utility Deleting lines which matches a pattern using sed command To delete lines which matches a pattern. '1d' is used in braces to indicate that its a command (to delete the specific line. Deleting the last line which matches a pattern using sed command bash-3.00# sed '/line/{1d.00# sed '/line/{$d. use the /d flag along with the pattern to be matched.

Sign up to vote on this title
UsefulNot useful