Unix – How to delete a specific line from a file in unix

sedunix

I have a very large file from which I need to delete a specific line (line number 941573 )

I'm somewhat new to this environment, but I've been googling the problem to no avail.

I've tried using the sed command as such, but it doesn't seem to be working

sed -e '941572,941574d' filenameX > newfilenameY

I've also tried

sed -e '941573d' filenameX > newfilenameY

Yet the 'newfilenameY' file and the original file 'filenameX' both still contain the line that I'm trying to delete. It's a fastq file, though I don't see how that would make any difference. Like I said I'm new to unix so maybe I've gotten the sed command wrong

Best Answer

d deletes a line/lines. So your second approach works.

$ sed '941573d' input > output

Long Example:

% for i in $(seq 1000000)
do
echo i >> input
done
% wc -l input
1000000 input
% sed '941573d' input > output
% wc -l output
999999 output
% diff -u input output                                      :(
--- input       2012-10-22 13:22:41.404395295 +0200
+++ output      2012-10-22 13:22:43.400395358 +0200
@@ -941570,7 +941570,6 @@
 941570
 941571
 941572
-941573
 941574
 941575
 941576

Short Example:

% cat input
foo
bar
baz
qux
% sed '3d' input > output
% cat output             
foo
bar
qux