LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-10-2010, 02:07 PM   #1
KFC123
Member
 
Registered: May 2009
Posts: 47

Rep: Reputation: 15
Using grep to remove line and write back to same file!?


I search the forum and get some help to remove a line starting with specific word with grep. Here is what I found

grep -v '^cc$' data.txt

Here I remove all lines with on 'cc' in that line. But I want the result write back to data.txt

I try several ways

grep -v '^cc$' data.txt > output.txt # works but to another file
echo `grep -v '^cc$' data.txt` > data.txt # didn't work, all carets gone, become one line
grep -v '^cc$' data.txt > data.txt # data.txt is empty after running this

How can I save the result of grep to the input file?

Thanks.
 
Old 02-10-2010, 02:13 PM   #2
Lord Mortus
LQ Newbie
 
Registered: Jan 2010
Posts: 13

Rep: Reputation: 0
You can try
grep -v '^cc$' data.txt >> data.txt

the >> appends the output so it will add it to the end of your target file.
 
Old 02-10-2010, 02:14 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Grep has some interesting options and is a good tool, but generally I wouldn't consider that a job for grep... That's a job for sed or awk (or perl even).

Code:
sed -i '/^cc/d' data.txt
If you're really insistent on grep--

Code:
grep -v '^cc$' data.txt > output.txt && mv -f output.txt data.txt

Last edited by rweaver; 02-10-2010 at 02:19 PM.
 
Old 02-10-2010, 02:34 PM   #4
KFC123
Member
 
Registered: May 2009
Posts: 47

Original Poster
Rep: Reputation: 15
I don't want to create a intermediate file but sed works perfectly for my case. Thanks a lot.

Quote:
Originally Posted by rweaver View Post
Grep has some interesting options and is a good tool, but generally I wouldn't consider that a job for grep... That's a job for sed or awk (or perl even).

Code:
sed -i '/^cc/d' data.txt
If you're really insistent on grep--

Code:
grep -v '^cc$' data.txt > output.txt && mv -f output.txt data.txt
 
Old 02-10-2010, 03:06 PM   #5
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Sed is a spectacular application, you can really replace grep with it entirely if you want to.

Check out the sed1line.txt for some spectacular examples of sed being a handy tool.
 
  


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
Using CAT / GREP to remove a set of lines from a file wesgarner Linux - Newbie 13 11-14-2009 11:41 AM
How to grep two strings in one line in a file? thomas2004ch Linux - Newbie 5 09-15-2009 05:49 AM
How to grep more than one line from a file markotitel Linux - Software 10 09-03-2009 07:33 AM
remove all links from a file with grep steve51184 Linux - Software 14 02-04-2009 12:09 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 11:51 AM


All times are GMT -5. The time now is 08:13 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