Sed is a powerful stream editor, typically used for editing large amounts of data by providing a simple command. Sed is also used for sophisticated searches, where the Regular Expressions are used. You can use sed to: Automate editing actions to be performed on one or more files

Simplify the task of performing the same edit on multiple files

Converting one data format to another For example the following command: # sed -i.bup '/^#/d;/^$/d' myfile 1 # sed -i.bup '/^#/d;/^$/d' myfile Do the following: Edit the file in place

Delete lines started with # (/^#/d)

Delete blank lines (/^$/d)

Create a backup file with bup extension To learn sed , we need to understand the syntax and learn by examples Print command Print all file

# sed 'p' [file] 1 # sed 'p' [file]

The above displays every line twice because it dump the file content to stdout while read it, to remove the stdout add –n:

# sed –n 'p' [file] 1 # sed –n 'p' [file]

Print lines 1-5

# sed -n '1,5p' [file] 1 # sed -n '1,5p' [file]

(if we remove the –n flag we get all file output to stdout but only the matched line will displayed twice)

Find line started with ‘root’ and print it

# sed -n '/^root/p' /etc/passwd 1 # sed -n '/^root/p' /etc/passwd

Print from line 15 to the end:

# sed -n '15,$p' /etc/passwd 1 # sed -n '15,$p' /etc/passwd

Print all except from line 15 to the end:

# sed -n '15,$!p' /etc/passwd 1 # sed -n '15,$!p' /etc/passwd

print lines start with ‘a’ followed by number

# sed -n '/^a[0-9]/p' /etc/passwd 1 # sed -n '/^a[0-9]/p' /etc/passwd

print lines with exactly 3 characters