Reputation: 5
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:
- Off through the new day's mist I run
- Off from the new day's mist
- I have come
- I hunt
I would like this output:
- Off through the new day's mist I run
- 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
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
Reputation: 75458
Instead of deleting, just replace the line with empty value:
sed -i "${deb},${end}s/.*//" body.txt
Upvotes: 2