sed command examples to delete or remove specific lines in linux
Here file is the file name.
[root@machine1 ~]# cat > file
first
second
third
fourth
fifth
END
[root@machine1 ~]# cat file
first
second
third
fourth
fifth
END
Here 1 is the line number and d is the option to delete first line
[root@machine1~]# sed '1d' file
second
third
fourth
fifth
END
[root@machine1 ~]# cat file
first
second
third
fourth
fifth
END
This will not remove in the orignal file, to remove in original use -i option
[root@machine1 ~]# sed -i '1d' file
[root@machine1 ~]# cat file
second
third
fourth
fifth
END
We also have the option to redirect it to the other file
[root@machine1 ~]# sed '2d' file > file1
To delete a last line in linux.
[root@machine1 ~]# sed '$d' file
second
third
fourth
fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
END
[root@machine1 ~]# sed -i '$d' file
[root@machine1 ~]# cat file
second
third
fourth
fifth
Delete for specific range. here i delete from 2nd to 4th line
[root@machine1 ~]# sed '2,4d' file
second
[root@machine1 ~]# cat file
second
third
fourth
fifth
Delete except first line
[root@machine1 ~]# sed '1!d' file
second
Delete except last line
[root@machine1 ~]# sed '$!d' file
fifth
Delete except specific line range from 2 to 4
[root@machine1 ~]# sed '2,4!d' file
third
fourth
fifth
Delete first and last line, here ; specify multiple conditions
[root@machine1 ~]# sed '1d;$d' file
third
fourth
Delete blank or empty lines
[root@machine1 ~]# sed '/^$/d' file
second
third
fourth
fifth
Delete line starts with pariticular characters
[root@machine1 ~]# sed '/^f/d' file
second
third
Delete line end with particular character
[root@machine1 ~]# sed '/d$/d' file
fourth
fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
LINUX
UBUNTU
Delete lines in upper case
[root@machine1 ~]# sed '/^[A-Z]*$/d' file
second
third
fourth
fifth
Delete lines that contain a specific pattern here i delete a line starts with a pattern UBUNTU
[root@machine1 ~]# sed '/UBUNTU/d' file
second
third
fourth
fifth
LINUX
Delete line starting from a pattern till the last line.
[root@machine1 ~]# sed '/fifth/,$d' file
second
third
fourth
Delete last line only if it contains a specific pattern
[root@machine1 ~]# sed '${/UBUNTU/d;}' file
second
third
fourth
fifth
LINUX
prints specific line which contain a pattern fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
LINUX
UBUNTU
[root@machine1 ~]# sed -n '/fifth/p' file
fifth
To print between 2 patterns
[root@machine1 ~]# sed -n '/fifth/,/UBUNTU/p' file
fifth
LINUX
UBUNTU
Here file is the file name.
[root@machine1 ~]# cat > file
first
second
third
fourth
fifth
END
[root@machine1 ~]# cat file
first
second
third
fourth
fifth
END
Here 1 is the line number and d is the option to delete first line
[root@machine1~]# sed '1d' file
second
third
fourth
fifth
END
[root@machine1 ~]# cat file
first
second
third
fourth
fifth
END
This will not remove in the orignal file, to remove in original use -i option
[root@machine1 ~]# sed -i '1d' file
[root@machine1 ~]# cat file
second
third
fourth
fifth
END
We also have the option to redirect it to the other file
[root@machine1 ~]# sed '2d' file > file1
To delete a last line in linux.
[root@machine1 ~]# sed '$d' file
second
third
fourth
fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
END
[root@machine1 ~]# sed -i '$d' file
[root@machine1 ~]# cat file
second
third
fourth
fifth
Delete for specific range. here i delete from 2nd to 4th line
[root@machine1 ~]# sed '2,4d' file
second
[root@machine1 ~]# cat file
second
third
fourth
fifth
Delete except first line
[root@machine1 ~]# sed '1!d' file
second
Delete except last line
[root@machine1 ~]# sed '$!d' file
fifth
Delete except specific line range from 2 to 4
[root@machine1 ~]# sed '2,4!d' file
third
fourth
fifth
Delete first and last line, here ; specify multiple conditions
[root@machine1 ~]# sed '1d;$d' file
third
fourth
Delete blank or empty lines
[root@machine1 ~]# sed '/^$/d' file
second
third
fourth
fifth
Delete line starts with pariticular characters
[root@machine1 ~]# sed '/^f/d' file
second
third
Delete line end with particular character
[root@machine1 ~]# sed '/d$/d' file
fourth
fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
LINUX
UBUNTU
Delete lines in upper case
[root@machine1 ~]# sed '/^[A-Z]*$/d' file
second
third
fourth
fifth
Delete lines that contain a specific pattern here i delete a line starts with a pattern UBUNTU
[root@machine1 ~]# sed '/UBUNTU/d' file
second
third
fourth
fifth
LINUX
Delete line starting from a pattern till the last line.
[root@machine1 ~]# sed '/fifth/,$d' file
second
third
fourth
Delete last line only if it contains a specific pattern
[root@machine1 ~]# sed '${/UBUNTU/d;}' file
second
third
fourth
fifth
LINUX
prints specific line which contain a pattern fifth
[root@machine1 ~]# cat file
second
third
fourth
fifth
LINUX
UBUNTU
[root@machine1 ~]# sed -n '/fifth/p' file
fifth
To print between 2 patterns
[root@machine1 ~]# sed -n '/fifth/,/UBUNTU/p' file
fifth
LINUX
UBUNTU
No comments:
Post a Comment