I have an xml file. I am trying to remove only the blank lines from a range of lines. In this example I want to check lines 55-57 and remove any blank lines.

This needs to be done in a shell script over multiple files that are all xml. This is a RHEL 7 system.

I tried using sed, but wasn't able to strip the empty lines out.

I have written statements that get the appropriate line numbers, they are stored as vars in the shell script (start_line, finish_line)

Example File:

3. <Test></Test>
57.<!-- keep the comment -->
58. <KeepTag></KeepTag>

What can I do to remove the blank lines only from range 55-57?

  • 2
    Please be careful parsing XML with line-based tools, unless you know FOR SURE that the XML format will remain unchanged from your example. – Jeff Schaller Jun 17 '20 at 15:01
  • Thanks for the head up, it will remain unchanged, I insert it with the script, I am writing a removal piece – Enter Strandman Jun 17 '20 at 15:03
  • sededitor can do this easily : sed -E '55,57{/\S/!d;}' – Rakesh Sharma Jun 18 '20 at 2:04

You could do this easily with :

awk 'FNR>=55 && FNR<=57 && NF==0{next}1' file

This uses the usual awk shorthand notation that a 1 in the "main program body" (i.e. outside rule-specific code in { ... }) means "print the current line including any modifications performed by rules".

However, it bypasses this print command by issuing the next command if the following criteria apply:

  • The line number is between (and including) 55 and 57 (condition: FNR>=55 && FNR<=57, with FNR being the per-file line-number -- as opposed to NR, the global line number, which can differ if you use awk to process multiple files in one call!)


  • the line contains no text, indicated by NF, the number of content fields, being zero.

Because of this "bypassing", the output generated by awk will exclude empty lines if they fall in the specified line range.

  • Syntax error, on > – Enter Strandman Jun 17 '20 at 14:59
  • OOps, typo fixed – Gilles Quenot Jun 17 '20 at 14:59
  • 1
    This removed the comments as well. I need to keep anything else and ONLY remove the blank lines. – Enter Strandman Jun 17 '20 at 15:04
  • Post edited accordingly – Gilles Quenot Jun 17 '20 at 15:06
  • 1
    I think something like awk 'NR>=55 && NR<=57 && NF==0 {next} 1' file might be what the OP wants ... – AdminBee Jun 17 '20 at 15:09

With sed:

sed "$start_line","$finish_line"'{/^$/d;}' file
  • tried this, it did nothing, sticking with the awk from @AdminBee – Enter Strandman Jun 17 '20 at 15:18

With sed, you can do it with address range

sed '55,57{/^$/d}' file

this will apply d command (delete) on selected empty line (/^$/) between line 55 and 57 (55,57). I am not able to test it, but I think you must add a ';' after the d command in macos.


sed -i '55,57{/^$/d;}' file

if you want to modify your file locally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.