I am trying to remove a single line (the first match) towards the beginning of a 250GB file. The command i have is this:
sed -i '0,/matchstring/{/matchstring/d;}' file
This works great on a smaller file, but on the big file, it never exits. I understand there is q
to tell sed to exit early, but I can't figure out how to add that into what I have here.
Since OP mentioned in the comment that
... something that replaces it with blanks works great, the line doesn't need to be removed.
And the following solution assumes the line is near to the begining of the file (otherwise we need a bit more tricks to get the exact offset of the line and the intermediate "header" file can be avoided).
[STEP 101] $ cat file
hello 1
hello 2
hello 3
foo matchstring bar
hello 4
hello 5
hello 6
[STEP 102] $ sed '/matchstring/{s/./ /g;q;}' file > header
[STEP 103] $ cat header
hello 1
hello 2
hello 3
[STEP 104] $ dd conv=notrunc if=header of=file
0+1 records in
0+1 records out
44 bytes copied, 0.000892102 s, 49.3 kB/s
[STEP 105] $ cat file
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
[STEP 106] $
And if it works by converting the matched line into a comment line, just change s/./ /g
to, for example, s/./#/
in the sed
command.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments