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 :) |
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 Code:
$ sed -i.bck '/hello/d' file_* |
We're talking thousands of files, with no common file names such as an extension or a prefix.
|
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' {} \; |
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' {} \; ? |
why would he need the find command? it seems to just cd to /home/Database/
and Code:
$ sed -i '/hello/d' * Tabby |
Oh. Wow.
Yeah atjurhs, heh, that did it. Thanks buddy. |
Quote:
Edit: too late... glad it worked! |
ooooh, thanks colucix :) I was pretty sure you had a reason for it, just didn't know what it was
|
Quote:
So your command: Code:
find . -type f -exec sed -i.bck '/hello/d {} \; |
Quote:
Code:
find . -name \*.bck -delete Code:
sed -i.GHDG76FD4 's/hello/d' *.txt |
Quote:
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 03:12 PM. |