Reputation: 343
How to delete a range of lines in a CSV file except a line(or few lines which or not consecutive) in between the given range... Lets say I need to delete the first five lines of a CSV file but keep the 3rd line as is, how do you achieve this. I tried to get this using SED but no help.
sed '1,5d;3!d' filenname.CSV > tempfilename.txt
I know I can do this by giving the range as
sed '1,2d;4,5d' filenname.CSV > tempfilename.txt
However, was wondering if there is a work around that i can specify a range to delete and give the exception of a line(or lines which or not consecutive) in between
Please correct if I am wrong.
Thanks in advance, Dhruuv
Upvotes: 2
Views: 1016
Reputation: 58558
This might work for you:
sed '1,5{3!d}' file
or:
sed '1,5!b;3!d' file
or:
sed '3b;1,5d' file
Upvotes: 2
Reputation: 77185
1_CR has the answer you are looking for but for fun you can use awk
.
awk '!(NR>0 && NR<6) || NR==3' file
Ignores line 1 to 5 but prints line 3 and all remaining lines.
Upvotes: 3
Reputation: 23394
Recast your sed
example to the below
sed '1,5{3p;d}' filenname.CSV > tempfilename.txt
Or to print the 3rd and 4th lines while deleting everything else from the range 1-5
sed '1,5{3p;4p;d}' filenname.CSV > tempfilename.txt
Upvotes: 5
Reputation: 240639
perl -ne 'print unless 1..5 and not 3..3' filename.CSV > tempfilename.txt
would work if you can use perl instead of sed.
Upvotes: 0