LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 12-10-2012, 03:12 PM   #1
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Rep: Reputation: Disabled
Grep(?) question I can't wrap my mind around


Hi.
I'm trying to remove lines, from multiple files, which contain a certain word. I don't want the file removed, only the line containing my word. I'm not sure if grep would be the best for this, or sed. Either way I can't really seem to figure it out. I can do it if it's from just one file, but I can't seem to do it for all files at once. There are too many to do it one by one..
Thanks in advanced
 
Old 12-10-2012, 03:21 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
If you want to remove lines you have to use sed, which is an editor, whereas grep is a tool to simply match a pattern or a regular expression. sed can accept multiple arguments and work on multiple files all at once. For example:
Code:
$ cat > file_one
hello world
goodbye
$ cat > file_two
hello mom
farewell
$ sed -i '/hello/d' file_*
$ cat file_one
goodbye
$ cat file_two
farewell
$
Be careful when using the -i option of sed to edit the file in place. Eventually you can use something like -i.bck to save a backup copy of the original files, review your results and then remove the backup copies if all is ok. Example:
Code:
$ sed -i.bck '/hello/d' file_*
Hope this helps.
 
1 members found this post helpful.
Old 12-10-2012, 03:23 PM   #3
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Original Poster
Rep: Reputation: Disabled
We're talking thousands of files, with no common file names such as an extension or a prefix.
 
Old 12-10-2012, 03:29 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Well, how do you select the files all at once? Are they all the files inside a directory structure? In any case you can use find in conjunction with sed and refine your search criteria to find all the files you want to modify:
Code:
find . -type f -exec sed -i.bck '/hello/d' {} \;
This will edit (and save a backup copy) all the files under the current working directory. Anyway, please describe better your situation so that we can give a more suitable advice.
 
1 members found this post helpful.
Old 12-10-2012, 03:32 PM   #5
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Original Poster
Rep: Reputation: Disabled
Sorry.
Yes, all of the files are in a directory.
Let's just say /home/Database is where all the files are.
so /home/Database/dkjkdjd
/home/Database/dkjkdjdskj
/home/Database/dasdjkad
etc. random file names.

Looking to remove all lines containing a specific word, let's just keep using 'hello'.

Edit: so.. say I'm in that dir.
find . -type f -exec sed -i.bck '/hello/d' {} \;
?

Last edited by Ziddy; 12-10-2012 at 03:34 PM.
 
Old 12-10-2012, 03:40 PM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 133

Rep: Reputation: Disabled
why would he need the find command? it seems to just cd to /home/Database/

and

Code:
$ sed -i '/hello/d' *
but I am a little new at this

Tabby
 
1 members found this post helpful.
Old 12-10-2012, 03:43 PM   #7
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Original Poster
Rep: Reputation: Disabled
Oh. Wow.
Yeah atjurhs, heh, that did it. Thanks buddy.
 
Old 12-10-2012, 03:45 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by atjurhs View Post
why would he need the find command? it seems to just cd to /home/Database/
Of course, but it was not clear the files were inside a single directory with no sub-directories. The only exception is if the list of files is really huge, it can trigger the "argument list too long" error. In that case the find command becomes useful, since sed is executed on every single file one at a time.

Edit: too late... glad it worked!
 
1 members found this post helpful.
Old 12-10-2012, 04:25 PM   #9
atjurhs
Member
 
Registered: Aug 2012
Posts: 133

Rep: Reputation: Disabled
ooooh, thanks colucix I was pretty sure you had a reason for it, just didn't know what it was
 
Old 12-10-2012, 05:04 PM   #10
EDDY1
Senior Member
 
Registered: Mar 2010
Location: Oakland,Ca
Distribution: DebianSqueeze, winsxp, wins7, Debian wheezy, LFS 7.2
Posts: 4,658

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by colucix View Post
Of course, but it was not clear the files were inside a single directory with no sub-directories. The only exception is if the list of files is really huge, it can trigger the "argument list too long" error. In that case the find command becomes useful, since sed is executed on every single file one at a time.

Edit: too late... glad it worked!
Although there was 1 solution given, this feels like a cliff-hanger, the movie ended leaving you hanging waiting for sequel.

So your command:
Code:
find . -type f -exec sed -i.bck  '/hello/d {} \;
would backup these files to pwd, what would've been the other solution or should I say what's next?
 
Old 12-11-2012, 02:38 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by EDDY1 View Post
Although there was 1 solution given, this feels like a cliff-hanger, the movie ended leaving you hanging waiting for sequel.
Actually there is no sequel, except for the backup files as you noticed. After creating them, you can easily delete by means of
Code:
find . -name \*.bck -delete
This assumes there was no .bck files previously, but you can always choose a random string as suffix to be sure, e.g.
Code:
sed -i.GHDG76FD4 's/hello/d' *.txt
 
Old 12-12-2012, 01:48 PM   #12
EDDY1
Senior Member
 
Registered: Mar 2010
Location: Oakland,Ca
Distribution: DebianSqueeze, winsxp, wins7, Debian wheezy, LFS 7.2
Posts: 4,658

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by colucix View Post
Actually there is no sequel, except for the backup files as you noticed. After creating them, you can easily delete by means of
Code:
find . -name \*.bck -delete
This assumes there was no .bck files previously, but you can always choose a random string as suffix to be sure, e.g.
Code:
sed -i.GHDG76FD4 's/hello/d' *.txt
Ok I got it now.
The "find" command searches all files & directories.
-i.bac makes a backup of the matching files described bringing it to pwd as in the example '/hello/d.
At that point job is done unless you want to delete the .bac
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Another awk question: un-word-wrap lines David the H. Linux - General 16 12-16-2007 01:12 PM
"enscript --word-wrap" does not wrap line of text file powah Linux - General 3 05-16-2006 09:12 PM
grep question Hockeyfan Linux - General 1 05-15-2004 02:09 AM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM


All times are GMT -5. The time now is 08:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration