LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep(?) question I can't wrap my mind around (https://www.linuxquestions.org/questions/linux-newbie-8/grep-question-i-cant-wrap-my-mind-around-4175440866/)

Ziddy 12-10-2012 03:12 PM

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 :)

colucix 12-10-2012 03:21 PM

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.

Ziddy 12-10-2012 03:23 PM

We're talking thousands of files, with no common file names such as an extension or a prefix.

colucix 12-10-2012 03:29 PM

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.

Ziddy 12-10-2012 03:32 PM

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' {} \;
?

atjurhs 12-10-2012 03:40 PM

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

Ziddy 12-10-2012 03:43 PM

Oh. Wow.
Yeah atjurhs, heh, that did it. Thanks buddy.

colucix 12-10-2012 03:45 PM

Quote:

Originally Posted by atjurhs (Post 4846528)
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!

atjurhs 12-10-2012 04:25 PM

ooooh, thanks colucix :) I was pretty sure you had a reason for it, just didn't know what it was

EDDY1 12-10-2012 05:04 PM

Quote:

Originally Posted by colucix (Post 4846533)
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?

colucix 12-11-2012 02:38 AM

Quote:

Originally Posted by EDDY1 (Post 4846584)
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
;)

EDDY1 12-12-2012 01:48 PM

Quote:

Originally Posted by colucix (Post 4846776)
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


All times are GMT -5. The time now is 09:39 AM.