LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2010, 04:43 PM   #1
mohamad
LQ Newbie
 
Registered: Jun 2010
Posts: 11

Rep: Reputation: 0
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
 
Old 06-10-2010, 04:49 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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

Last edited by David the H.; 06-10-2010 at 04:54 PM.
 
Old 06-10-2010, 04:54 PM   #3
mohamad
LQ Newbie
 
Registered: Jun 2010
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 06-10-2010, 05:09 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
Old 06-10-2010, 05:12 PM   #5
mohamad
LQ Newbie
 
Registered: Jun 2010
Posts: 11

Original Poster
Rep: Reputation: 0
thank you

i wanna kiss you so bad... too bad im married... ive spent atleast 3 hrs for this double quote.. thanks alot
 
Old 06-10-2010, 05:19 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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.
 
Old 06-10-2010, 05:20 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 09:41 PM
deleting line by line from a file netpumber Programming 12 04-22-2010 08:22 AM
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 04:42 AM
sed to delete a line for a word and line above cmontr Programming 11 07-03-2008 09:33 AM
deleting files containing a specific word Sleb Programming 5 05-25-2006 02:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration