DELETE ONE OR MORE LINES IN FILES IN LINUX

Sed – Delete one or more lines from a file

Unix utility SED provides an effective and a versatile way of deleting one or more lines from a designated file to match the needs of the user. This Unix command is used for command line processing. This utility can be used to delete expressions from a file which can be identified by a specifying delimiter (like comma, tab, space, etc.), by line number, by searching for a string, expression or the address of a line in the syntax of SED.

[Sed] Delete one or more lines from a file

Here is how to remove one or more lines from a file.

Syntax:

sed '{[/]<n>|<string>|<regex>[/]}d' <fileName>       
sed '{[/]<adr1>[,<adr2>][/]d' <fileName>
  • /…/=delimiters
  • n = line number
  • string = string found in in line
  • regex = regular expression corresponding to the searched pattern
  • addr = address of a line (number or pattern )
  • d = delete

Examples

Remove the 3rd line:

sed '3d' fileName.txt

Remove the line containing the string “awk”:

sed '/awk/d' filename.txt

Remove the last line:

sed '$d' filename.txt

Remove all empty lines:

sed '/^$/d' filename.txt       
sed '/./!d' filename.txt

Remove the line matching by a regular expression (by eliminating one containing digital characters, at least 1 digit, located at the end of the line):

sed '/[0-9/][0-9]*$/d' filename.txt

Remove the interval between lines 7 and 9:

sed '7,9d' filename.txt

The same operation as above but replacing the address with parameters:

sed '/-Start/,/-End/d' filename.txt

The above examples are only changed at the display of the file (stdout1= screen).

For permanent changes to the old versions (<4) use a temporary file for GNU sed using the “-i[suffix]”:

sed -i".bak" '3d' filename.txt

SOURCE: http://en.kioskea.net/faq/1451-sed-delete-one-or-more-lines-from-a-file

 

WHAT I DID IN THE COMMAND ABOVE…

Remove the line containing the string “awk”:

sed '/awk/d' source.txt > distination.txt

ANOTHER SOURCE::::


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” (remove the /d to show the lines that the command will delete):

:g/profile/d

More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:

:g/^\s*$/d

To 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*"/d

Note that g! is equivalent to v, so you could also do the above with:

:v/^\s*"/d

The 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

Can we remove all even numbered lines in a file using this feature. can we do some kind of math in the pattern. (ex: \=line(“.”) % 2)

Not really, but you can do that in two steps:

:g/.*/if line('.')%2|call setline(line('.'), '===delete===')|endif
:g/^==delete==$/d

If you simply put delete inside the if statement all the lines will be deleted. Much faster solution is to record a macro “ddj” and play it over the file. You could delete lines from several different ranges:

:let range = range(10,15)+range(20,25)+range(30,35)
:g/.*/if index(range, line('.')) != -1|call setline(line('.'), '===delete===')|endif
:g/^===delete===$/d

But again I think the faster way to do that is to use :[range]d several times.

How would you instead of deleting, replace matched lines with a single newline between remaining lines?

SOURCE: http://vim.wikia.com/wiki/Delete_all_lines_containing_a_pattern

 

Advertisements
This entry was posted in LINUX and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s