I need to remove a specific line number from a file using a bash script.
I get the line number from the grep command with the -n option.
I cannot use sed for a variety of reasons, least of which is that it is not installed on all the systems this script needs to run on and installing it is not an option.
awk is out of the question because in testing, on different machines with different UNIX/Linux OS's (RHEL, SunOS, Solaris, Ubuntu, etc.), it gives (sometimes wildly) different results on each. So, no awk.
The file in question is just a flat text file, with one record per line, so nothing fancy needs to be done, except for remove the line by number.
If at all possible, I need to avoid doing something like extracting the contents of the file, not including the line I want gone, and then overwriting the original file.
Best Answer
Since you have
grep
, the obvious thing to do is:But it sounds like you don't want to use any temporary files - I assume the input file is large and this is an embedded system where memory and storage are in short supply. I think you ideally need a solution that edits the file in place. I think this might be possible with
dd
but haven't figured it out yet :(Update - I figured out how to edit the file in place with dd. Also
grep
,head
andcut
are needed. If these are not available then they can probably be worked around for the most part:Run it thusly:
Suffice to say that
dd
is a very dangerous tool. You can easily unintentionally overwrite files or entire disks. Be very careful!