I have a file with that contains a list of names ( exactly 1 name
per line. no spaces)
I need a script to find a particular name in the file and remove
the entire line.
ie
peter
joe
frank
pete
pearl
(sed script to remove pete)
peter
joe
frank
pearl
Part of my problem is that I can't figure out how to remove only pete (and not peter)
I tried this
search for the user called firstname
sed -e '/^firstname$/ d' > everyone.lis
This works.
Now I want to use a variable called name.
I tried
cat everyone.lis |sed -e '/^${name}$/ d' > everyone.lis
This did not work. Any suggestions ?
I'm not near a unix system right now, so I can't test it out, but try placing quotes (single or double) around your variable:
"$name"
The single quotes encapsulating your sed program prevents the shell
from evaluating variables before it executes it. Change those to double-quotes,
or in this case, you don't need any quotes.
Also, there is no advantage in using the extra cat process - just let
sed read the file directly instead of a pipe.
And while you do not need braces around the variable in this case, it never hurts.
You will not be able to redirect your output back to your input file. The first thing the shell will do, before it invokes sed, is to create an empty output file, and that will wipe out your input file before sed gets to process it.
sed /^$name$/d everyone.lis > newfile
mv newfile everyone.lis
angle bracket testing ...
word in angle brackets:
< word >
backslashed left angle brackets:
\< word >
backslashed both angle brackets:
\< word \>
line1 line2 in angle brackets (two lines):
< line1
line2 >
end of test
Quick Links:
Do you have
a UNIX Question?
Unix Home: Unix System Administration
Hints and Tips