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