Docsity

SED Command in UNIX, Schemes and Mind Maps of Computer Science

Toggle sidebar
bg1
1
SED - stream editor
SED command in UNIX stands for stream editor and it can perform lots of
functions on file like searching, find and replace insertion or deletion. Though
most common use of SED command in UNIX is for substitution or for find
and replace. By using SED you can edit files even without opening them,
which is much quicker way to find and replace something in file, than first
opening that file in VI Editor and then changing it.
SED is a powerful text stream editor. Can do insertion, deletion, search
and replace(substitution).
SED command in unix supports regular expression which allows it
perform complex pattern matching.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$ cat F3
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is
a powerful.
Replacing or substituting string : Sed command is mostly used to replace
the text in a file. The below simple sed command replaces the word “unix”
with “linux” in the file.
$sed 's/unix/linux/' F3
Here the “s” specifies the substitution operation. The “/” are delimiters. The
“unix” is the search pattern and the “linux” is the replacement string. By
default, the sed command replaces the first occurrence of the pattern in each
line and it won’t replace the second, third…occurrence in the line.
Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc
flags to replace the first, second occurrence of a pattern in a line. The below
pf3
pf4

Partial preview of the text

Download SED Command in UNIX and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

SED - stream editor SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.  SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).  SED command in unix supports regular expression which allows it perform complex pattern matching. Syntax: sed OPTIONS... [SCRIPT] [INPUTFILE...] Example: Consider the below text file as an input. $ cat F unix is great os. unix is opensource. unix is free os. learn operating system. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file. $sed 's/unix/linux/' F Here the “s” specifies the substitution operation. The “/” are delimiters. The “unix” is the search pattern and the “linux” is the replacement string. By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace the second, third…occurrence in the line. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below

command replaces the second occurrence of the word “unix” with “linux” in a line. $sed 's/unix/linux/2' F Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. $sed 's/unix/linux/g' F Replacing from nth occurrence to all occurrences in a line : Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth… “unix” word with “linux” word in a line. $sed 's/unix/linux/3g' F Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis. $ echo "Amrita Sai Institute Of Science and Technology" | sed 's/(\b[A-Z])/(\ 1 )/g' Replacing string on a specific line number : We can restrict the sed command to replace the string on a specific line number. An example is $sed '3 s/unix/linux/' F Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once. $sed 's/unix/linux/p' F Printing only the replaced lines : Use the - n option along with the /p print flag to display only the replaced lines. Here the - n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time. $sed - n 's/unix/linux/p' F

$ sed '12,$d' filename.txt

  1. To Delete pattern matching line Syntax: $ sed '/pattern/d' filename.txt Example: $ sed '/abc/d' filename.txt