2

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:

1.
2. 
3. <Test></Test>
        .
        .
        .
55.
56.
57.<!-- keep the comment -->
58. <KeepTag></KeepTag>

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

3
  • 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, 2020 at 15:01
  • Thanks for the head up, it will remain unchanged, I insert it with the script, I am writing a removal piece Jun 17, 2020 at 15:03
  • sededitor can do this easily : sed -E '55,57{/\S/!d;}' Jun 18, 2020 at 2:04

3 Answers 3

2

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!)

    and

  • 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.

11
  • Syntax error, on > Jun 17, 2020 at 14:59
  • OOps, typo fixed Jun 17, 2020 at 14:59
  • 1
    This removed the comments as well. I need to keep anything else and ONLY remove the blank lines. Jun 17, 2020 at 15:04
  • Post edited accordingly Jun 17, 2020 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, 2020 at 15:09
1

With sed:

start_line=55
finish_line=57
sed "$start_line","$finish_line"'{/^$/d;}' file
1
  • tried this, it did nothing, sticking with the awk from @AdminBee Jun 17, 2020 at 15:18
1

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.

use

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.