Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to Delete Certain Lines in a Range Using sed

Tags:

linux

sed

In a large file, I need to edit text and remove comments inside a particular range. For this simplified example, let's assume the range begins with _start_ and finishes at _end_.

I'm able to edit the text with no problem using a command like:

sed -i -r "/_start_/,/_end_/ s/SearchText/ReplaceText/" FileName

Please note the following (and let me know, of course, if any of my statements are inaccurate or misguided):

  • I used -i so that it would edit "FileName" in place, rather than write to a different file.
  • I used -r so that it would recognize extended regular expressions (which are not shown in the simplified example above, but which seem to be working correctly).
  • I used double-quotes so that it would correctly handle variables (also not shown in the simplified example above, but also working as expected).

That command above is doing exactly what I expect it to do. So I moved on to the second step of my process: a very similar command to remove comment lines within this range:

sed -i -r "/_start_/,/_end_/ /^#/ d" FileName

This, however, is having no effect: The lines that begin with # are not removed. Indeed, when I execute this command alone, absolutely nothing in my file is changed or deleted -- neither inside the range nor elsewhere.

In my searches on this site and elsewhere, I've found a lot of instructions on deleting lines using sed (instructions that I think I'm following correctly) -- but nothing about a failure such as I'm experiencing.

Can anyone advise me what I'm doing wrong here?

I'm very new to the UNIX/Linux environment, so I'm definitely open to alternate suggestions as to how to handle the issue. But just to satisfy my frustration, I'd love to know what's wrong with my sed command above.

like image 524
Joe DeRose Avatar asked Oct 24 '13 21:10

Joe DeRose


People also ask

How do you delete a specific line in a file using sed?

Deleting line using sed To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I delete a specific number of lines in Linux?

@KanagaveluSugumar sed -e '5d' file . The syntax is <address><command> ; where <address> can be either a single line like 5 or a range of lines like 5,10 , and the command d deletes the given line or lines. The addresses can also be regular expressions, or the dollar sign $ indicating the last line of the file.

How do I remove a specific line from a file in Unix?

To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.

Can we delete content in a file by using sed command?

There is no available to delete all contents of the file. How to delete all contents of the file using sed command.


2 Answers

The best source of information is often the man page. You can reach it with the command man sed.

d takes an address range according to the man page. An address can be a number, a /regexp/, or a number of other things. An address range is either one address or two addresses, separated by comma. You have been trying to use an address range and then an address.

As 1_CR pointed out, you can work around by using a block instead:

sed -i -r "/_start_/,/_end_/ {/^#/ d}" FileName

A block accepts an address range, and every command accepts an address range again, so you can combine the regexps.

like image 138
thiton Avatar answered Nov 15 '22 18:11

thiton


You need to change

sed -i -r "/_start_/,/_end_/ /^#/ d" FileName

to

sed -i -r "/_start_/,/_end_/{/^#/d}" FileName
like image 27
iruvar Avatar answered Nov 15 '22 19:11

iruvar