Pages

Saturday, December 21, 2013

SED- Stream Editor

1. How to Print all the lines in a file using sed
# sed 'p' test.txt
To print the Second line in a file
# sed -n '2 p' test.txt
To print from line 1 to line 4
# sed -n '2 p' test.txt
To Print from line 2 through the last line
# sed -n '2,$ p' test.txt
To Print lines matching the pattern “vasanth”
# sed -n '/vasanth/ p' test.txt
To Print lines starting from the 1st match of "Vasanth" until the 4th
# sed -n '/vasanth/,4 p' test.txt
To Print lines starting from the 1st match of "vasanth" until the last line
# sed -n '/vasanth/,$ p' test.txt
To Print lines starting from the line matching "Vasanth" until the line matching "Hemanth":
# sed -n '/Vasanth/,/Hemanth/ p' test.txt
To Print the line matching "Jason" and 2 lines immediately after that
# sed -n '/Vasanth/,+2 p' test.txt
DELETE LINES
To delete all the lines in a file
# sed 'd' test.txt
To delete only the two lines
# sed '2 d' test.txt
To Delete from line 1 through 4
# sed '1,4 d' test.txt
To Delete from line 2 through the last line
# sed '2,$ d' employee.txt
To delete only odd number of lines
# sed '1~2 d' test.txt
To delete lines matching the pattern "Sysadmin"
# sed '/Sysadmin/ d' test.txt
To delete lines starting from the 1st match of "Vasanth" until the 4th line
# sed '/Vasanth/,4 d' test.txt
To delete lines starting from the 1st match of "Vasanth" until the 4th line
# sed '/Vasanth/,4 d' test.txt
To delete lines starting from the 1st match of "Vasanth" until the last line
# sed '/Vasanth/,$ d' test.txt
To delete lines starting from the line matching "Vasanth" until the line matching "Hemanth":
# sed '/Vasanth/,/Hemanth/ d' test.txt
To delete lines starting from the line matching "Vasanth" and 2 lines immediately after that:
# sed '/Vasanth/,+2 d' test.txt
Useful Delete Examples
To delete all the empty lines from a file:
# sed '/^$/ d' test.txt
To delete all comment lines (assuming the comment starts with
  1. ):
# sed '/^#/ d' test.txt
To write the content of text.txt file to file test.txt (and display on screen):
# sed 'w test.txt' text.txt
To write the content of employee.txt file to output.txt file but not to screen:
# sed -n 'w output.txt' /etc/passwd
To write only the 2nd line:
# sed -n '2 w output.txt' /etc/passwd
Write lines 1 through 4:
# sed -n '1,4 w output.txt' /etc/passwd

To write from line 2 through the last line:
# sed -n '2,$ w output.txt' /etc/passwd
To write only odd numbered lines:
# sed -n '1~2 w output.txt' /etc/passwd
To Write lines matching the pattern "Vasanth":
# sed -n '/Vasanth/ w output.txt' /etc/passwd
To write lines starting from the 1st match of "root" until the 4th line:
# sed -n '/root/,4 w output.txt' /etc/passwd
To write lines starting from the 1st match of "Raj" until the last line:
# sed -n '/vasanth/,$ w output.txt' /etc/passwd 
To write lines starting from the line matching "vasanth" until the line matching "hemanth":
# sed -n '/vasanth/,/hemanth/ w output.txt' /etc/passwd
To write the line matching "vasanth" and the next 2 lines immediately after that:
# sed -n '/vasanth/,+2 w output.txt' /etc/passwd

No comments:

Post a Comment