LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   deleting a line containing the word (https://www.linuxquestions.org/questions/linux-newbie-8/deleting-a-line-containing-the-word-813418/)

mohamad 06-10-2010 03:43 PM

deleting a line containing the word
 
hello,
what would be the syntax to delete a line containg the word "word" from the file "file"
i want to delete the whole line and not only the word... and i do not know the number of the line so thats not what im looking for

David the H. 06-10-2010 03:49 PM

Code:

sed -i '/word/d' filename
The '-i' option is for editing the file in place. Run the command without it first to confirm that it's doing what you want.

Note also that this will remove all lines that contain the word. There's more trickery involved if you need to narrow it down to a single instance.

Edit: Learn more about sed here:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html

mohamad 06-10-2010 03:54 PM

it doesnt work
 
i just tried that but it doesnt work...
heres what im doing:

echo "Enter name: "
read name;
sed -i '/$name/d' register;;

whats suppose to happen is that when i enter a certain name that is already there in the register... its suppose to be deleted WITH the number that is on the same line

maybe this can help u help me

David the H. 06-10-2010 04:09 PM

Well, you didn't mention that this was for a script.

Your problem above is with shell quoting. Single quotes make everything inside them literal, so your '$' isn't read as a variable. You need to use double quotes here instead, which allows a few characters like this to still be interpreted.

There are lots of good primers on bash scripting out there. Such as this one.
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

And their section on quoting:
http://tldp.org/LDP/Bash-Beginners-G...ect_03_03.html

mohamad 06-10-2010 04:12 PM

thank you
 
i wanna kiss you so bad... too bad im married... ive spent atleast 3 hrs for this double quote.. thanks alot

David the H. 06-10-2010 04:19 PM

I should say though that while that script will work, it's not very safe. As I mentioned, sed will remove every line with that text string in it. So if, for example you run the command:
Code:

sed "/Pete/d" file
And your file has lines like this:

Code:

Pete Jones
John Peterson
Debra Petel Smythe

...all three names will be lost.

If you show us the exact format of the file, we may be able to make it safer using a proper regex.

pixellany 06-10-2010 04:20 PM

go to http://tldp.org
Get a copy of the Advanced Bash Scripting Guide and look up quoting


All times are GMT -5. The time now is 01:21 AM.