View Full Version : Is there a Shell Script program to delete one line of a file on the command line?
kevdog
February 28th, 2008, 01:46 PM
Looking for a one line shell script on the command line I could use to remove a line in a config file (for example the first or last line or line that contains xxxxx). Here are the examples Ive found thus far. Any better methods?
awk 'NR!=4' file1 > file2
cp /etc/apt/sources.list /etc/apt/sources.list_backup
grep -v “line you want to remove” /etc/apt/sources.list_backup > /etc/apt/sources.list
ghostdog74
February 28th, 2008, 02:34 PM
if they suit your purpose, then use them.
other method
head -3 file > newfile
more +5 file >> newfile
using sed
sed '4d' file
erginemr
February 28th, 2008, 03:37 PM
Hi kevdog,
I think, your solution:
grep -v "line you want to remove" /etc/apt/sources.list_backup > /etc/apt/sources.list
is the most elegant way to do it.
Alternatively, you can use:
sed "/line you want to remove/d" /etc/apt/sources.list_backup > /etc/apt/sources.list
Kindly see the attached example...
kevdog
February 28th, 2008, 06:39 PM
Just for my reference -- there is no way to avoid use of a temporary file (at least specifying one on the command line -- what happens internally within a buffer is different) other than with something similar to
sed '4d' file
Im not familar with the +5 part of this command:
more +5 file >> newfile
I read the man pages but they dont list this as an option. What exactly does this do?
scruff
February 28th, 2008, 06:47 PM
GNU sed includes a -i option for "inline" edits. No need for a temp backup file :)
kevdog
February 28th, 2008, 06:49 PM
So it would be something like this??:
sed -i "/line you want to remove/d" /etc/apt/sources.list
So hence the sources.list would be the same as the original, minus the line you removed?
scruff
February 28th, 2008, 09:34 PM
Yes that is correct.
ghostdog74
February 29th, 2008, 02:12 AM
I read the man pages but they dont list this as an option. What exactly does this do?
please read the man page again. you should see +num
it just simply means start from that line number (num)
Roguebantha
July 31st, 2012, 09:13 PM
I accomplished deleting the last line of a file like this:
$cp Example1 ./tmp
$head --lines=-1 tmp > Example1
In which running head --lines=-1 shows all of the lines except the last one.
Bachstelze
July 31st, 2012, 10:21 PM
GNU sed includes a -i option for "inline" edits. No need for a temp backup file :)
And even without GNU sed, the same thing can be achieved with ed (this is why POSIX sed does not include a similar functionality: it would be redundant since it's already in ed).
nothingspecial
July 31st, 2012, 10:23 PM
Old thread.
Closed.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.