LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-06-2006, 06:05 PM   #1
shooters
LQ Newbie
 
Registered: Dec 2005
Distribution: Ubuntu 6.06 (Dapper)
Posts: 18

Rep: Reputation: 0
Look for a string in a file


Hi,

I'd like to do a script that would look for the string "E189" in a file, and if it finds it, simply delete the file...

Thanks
 
Old 01-06-2006, 07:40 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
If you just want to do this a few times you can just type:

rm -i `grep -l E189 *`

I think you have to use the back quote (on the same key as the ~). The rm -i asks your permission before deletion. The grep -l lists any filenames that contain the string "E189". If you only want to search certain files, you can use something other than *.

Now if you want a script to reuse all the time, make a file like grep_rm.sh and chmod +x grep_rm.sh.

Then put this inside the file:

#!/bin/bash
rm -i `grep -l E189 $1`


Then you would invoke it with ./grep_rm.sh *.txt. Be careful not to just use * or you'll delete your script too!
 
Old 01-06-2006, 07:43 PM   #3
bosewicht
Senior Member
 
Registered: Aug 2003
Location: Honolulu, HI
Distribution: Arch
Posts: 1,380

Rep: Reputation: 47
I think he was looking more towards sed/awk. What he is looking for is text in a file, i believe not the filename. Look online for sed/awk tutorials.
 
Old 01-06-2006, 08:00 PM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Check out the "strings" command instead of "grep".
 
Old 01-06-2006, 08:06 PM   #5
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
In the script above, grep -l E189 * searches within all files and returns the file name of files that contain "E189". He said he wanted to delete any file that contains "E189". The above script will delete any file that contains "E189". Well, at least the first file, I don't know if it will continue to search once it finds the first instance...

Last edited by pljvaldez; 01-06-2006 at 08:08 PM.
 
Old 01-06-2006, 08:11 PM   #6
valencequark
Member
 
Registered: Aug 2004
Location: greater dc metro area
Distribution: various, mostly fedora
Posts: 79

Rep: Reputation: 16
Quote:
Originally Posted by pljvaldez
In the script above, grep -l E189 * searches within all files and returns the file name of files that contain "E189". He said he wanted to delete any file that contains "E189". The above script will delete any file that contains "E189". Well, at least the first file, I don't know if it will continue to search once it finds the first instance...

I think it will keep searching/deleting beyond the initial instance.

Edit: It does, I just tried it.

Last edited by valencequark; 01-06-2006 at 08:19 PM.
 
Old 01-06-2006, 08:31 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
In the debate over what the OP wanted, there's a key person missing....the OP!

When you say find something "in a file" that means to me that you want to look at the contents, not the title.

Try this (with correct syntax, of course):
if (echo `grep pattern filename` -ne NULL) then
rm -f filename
fi
 
Old 01-07-2006, 02:51 AM   #8
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
Yes, the script I posted above searches inside files for the string, not filenames. The output is a filename (which is being fed into the rm command).
 
  


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
Remove last character from file/string linuxchump Programming 34 06-08-2009 05:01 AM
Strange string in log file ne21 Linux - Security 2 04-18-2004 05:23 AM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 05:17 AM
Appending a string to a file JStew Linux - General 13 03-04-2003 04:48 PM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 08:13 PM


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