sed (stream editor) is a Unix utility that parses text files and implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line. It was developed from 1973 to 1974 as a Unix utility by Lee E. McMahon of Bell Labs, and is available today for most operating systems.
How to remove Blank Lines or some line starting from particular pattern.
Here Suppose I want to remove the Blank Lines and Lines starting with #.
Original File:
[root@gateway ~]# cat a
Default:
# sleep_after_fork 0
# TAG: max_filedesc
# The maximum number of open file descriptors.
#
# WARNING: Changes of this value isn't respected by reconfigure
command. This value should be changed only if there isn't
# any active squid process.
#
NOTE: This option is only supported by system with poll()
# or epoll(). You can set this value by --with-maxfd during
# compilation on system whith uses select().
#
The maximum value for max_filedesc is set by --with-maxfd during
# compilation.
#
#Default:
max_filedesc 1024
[root@gateway ~]#
For removing Blank Lines:
[root@gateway ~]# sed '/^$/d' a > b
[root@gateway ~]# cat bDefault:
# sleep_after_fork 0
# TAG: max_filedesc
# The maximum number of open file descriptors.
#
# WARNING: Changes of this value isn't respected by reconfigure
command. This value should be changed only if there isn't
# any active squid process.
#
NOTE: This option is only supported by system with poll()
# or epoll(). You can set this value by --with-maxfd during
# compilation on system whith uses select().
#
The maximum value for max_filedesc is set by --with-maxfd during
# compilation.
#
#Default:
max_filedesc 1024
[root@gateway ~]#
Now For removing lines starting with # :
[root@gateway ~]# sed '/^#/d' b > a
[root@gateway ~]# cat a
Default:
command. This value should be changed only if there isn't
NOTE: This option is only supported by system with poll()
The maximum value for max_filedesc is set by --with-maxfd during
max_filedesc 1024
[root@gateway ~]#
The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
For example, to delete all lines containing "profile" (the first command is optional; it shows the lines that the second command will delete):
:g/profile :g/profile/dMore complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:
:g/^\s*$/dTo delete all lines that do not contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:
:g!/^\s*"/dNote that g! is equivalent to v, so you could also do the above with:
:v/^\s*"/dThe next example shows use of \| ("or") to delete all lines except those that contain "error" or "warn" or "fail" (:help pattern):
:v/error\|warn\|fail/d
Kuldeep Sharma