LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-11-2007, 08:12 AM   #1
supasharp
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Rep: Reputation: 0
Grep and remove


Quick question, does the grep command have an extension where I can find and remove a line from a text file.

I have a text files with numbers (one per line), and I need to search and remove a whole line, and space if found

Any ideas
 
Old 06-11-2007, 08:35 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
At least two methods:

"numfile" has 6 lines, containing numbers 1-6

Code:
mherring@1[PLAY]$ cat numfile|grep -v 4
1
2
3
5
6
mherring@1[PLAY]$ cat numfile|sed  /4/d
1
2
3
5
6
(Then use > to write a new file)

man grep and man sed for the details
(For sed, the best tutorial I have found is: http://www.grymoire.com/Unix/Sed.html --just in case learning sed from the man pages does not appeal to you.... )

Last edited by pixellany; 06-11-2007 at 08:37 AM.
 
Old 06-11-2007, 09:21 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany
Code:
mherring@1[PLAY]$ cat numfile|grep -v 4
...
mherring@1[PLAY]$ cat numfile|sed  /4/d
...
UUOC.
Code:
1) sed  /4/d numfile
2) grep -v 4 numfile
 
Old 06-12-2007, 08:23 AM   #4
supasharp
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Ok,

I created a file call "numbers", that features the following lines:
12345
12828
12338
12343

I run the command:
Code:
cat numbers|sed /12345/d >numbers
that results in a blank txt file
I run the code:
Code:
echo cat numbers|sed /12345/d >numbers
and I get a blank document?
 
Old 06-12-2007, 08:45 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
You need to enclose the sed argument string in single quotes. It is a general rule which helps to "disambiguate" the syntax. In this case, I don't know why it does not work without the quotes.
(I was sloppy--and got away with it---in the other example.)

Code:
mherring@1[PLAY]$ cat numfile
12345
45678
34567
21345
23478
mherring@1[PLAY]$ cat numfile|sed '/12345/d' >newnum
mherring@1[PLAY]$ cat newnum
45678
34567
21345
23478
 
Old 06-12-2007, 09:28 AM   #6
supasharp
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
I tried as you said, and that works fine when creating a new file.

The only problem as the file name needs to be the same so if I try this

Code:
cat numbers|sed '/12345/d' > numbers
the file comes out blank. Any ideas?
 
Old 06-12-2007, 09:44 AM   #7
moxieman99
Member
 
Registered: Feb 2004
Distribution: Dabble, but latest used are Fedora 13 and Ubuntu 10.4.1
Posts: 425

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by supasharp
I tried as you said, and that works fine when creating a new file.

The only problem as the file name needs to be the same so if I try this

Code:
cat numbers|sed '/12345/d' > numbers
the file comes out blank. Any ideas?
---------------
Have the new file generated in a different directory
 
Old 06-12-2007, 09:48 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by supasharp
I tried as you said, and that works fine when creating a new file.

The only problem as the file name needs to be the same so if I try this

Code:
cat numbers|sed '/12345/d' > numbers
the file comes out blank. Any ideas?
don't do this. Also your syntax is useless use of cat. If you have GNU sed, you can use the -i switch
Code:
sed -i '/12345/d' numbers
if not, you will just have to output to a temp file, then rename it back to original.
another way:
Code:
awk '!/12345/{print}' file > outfile
or
Code:
awk '!/12345/' file > outfile
 
Old 06-12-2007, 10:38 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Hi,

And another way of doing it (using sed):

sed -i -n '/12345/!p' numbers

or (old style sed):

sed -n '/12345/!p' numbers > new.numbers
 
Old 06-12-2007, 11:16 AM   #10
supasharp
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for all the help
I just added that line to a script to over write the old file with the new one
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
grep: grep for two substrings? eur0dad Linux - General 2 08-17-2006 04:03 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
remove text with grep craigdolson Linux - General 6 04-21-2004 03:25 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:44 PM.

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
Open Source Consulting | Domain Registration