debas
debas

Reputation: 5

BASH - Delete lines and replace them by blank lines

I have a text file and I would like to delete some lines and replace them by blank ones. Example: If I have this file:

  1. Off through the new day's mist I run
  2. Off from the new day's mist
  3. I have come
  4. I hunt

I would like this output:

  1. Off through the new day's mist I run
  2. I hunt

I already tried this command but there is no blank lines:

sed -i "$deb,$end"'d' body.txt (where $deb and $end are the number of the lines)

Do you have any clue on how to get this output format using bash, sed or awk? (I cannot use Perl)

Thank you!

Upvotes: 0

Views: 89

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Try this awk command,

awk -v var1=2 -v var2=3 'NR==var1{gsub (/.*/,"")} NR==var2{gsub (/.*/,"")}1' file

Example:

$ awk -v var1=2 -v var2=3 'NR==var1{gsub (/.*/,"")} NR==var2{gsub (/.*/,"")}1' rr.txt
        Off through the new day's mist I run


        I hunt

OR

$ awk -v var1=$deb -v var2=$end 'NR==var1{gsub (/.*/,"")} NR==var2{gsub (/.*/,"")}1' rr.txt
        Off through the new day's mist I run


        I hunt

Upvotes: 0

konsolebox
konsolebox

Reputation: 75458

Instead of deleting, just replace the line with empty value:

sed -i "${deb},${end}s/.*//" body.txt 

Upvotes: 2

Related Questions