How to search multiple files w/ SED then echo back the filenames and results???
I am tearing my hair out with this one.
I am writing a script to let me search a variable number of files for a string, then output the results like this (bold/italics formatting is unimportant): filename: search result An example command would look like this: # powersearch -f searchterm file1 file2 file3 ... results: name of file where match was found1: searchterm name of file where match was found3: searchterm name of file where match was found9: searchterm ...(etc) I cannot for the life of me figure out how to do this. Here is what I have written so far. The first case (-w|w) works fine. But on the second case (-f|f) I cannot figure out how to search through all the files and get output like above. I appreciate any help... Code:
while test -n "$1"; do |
Moin,
Do you mean this? Code:
... |
Just how is it failing? What kind of output do you get when you try the code as is?
Is this a bash script, or do you want it to be shell-agnostic? In bash you can use "#!/bin/bash -x" on the first line to turn on verbose output for debugging. I'm away from home and don't have access to a linux machine right now, so I can't do much to help you directly, but I do notice one thing that you probably want to fix. Since '$@' represents all the command line arguments together, using it here means that you're also including your '$1' and '$2' entries as file names. Also, is there a chance that your file names will have spaces in them? That could throw off the search. A good way to get around that is to change the IFS environment variable to ignore spaces as separators in the script. |
Quote:
Code:
user@PN-SLEETSTONE ~/scripts $ ./myGrep_v2 -f beth phoneNumbers doc The file names on my personal machine have spaces, but the files at work don't, so that's not an issue. This (above) is just to test it out. Is there anyway to improve that loop so that it ignores $1 as a file name? Or a way to make SED completely suppress any error output? |
And why would one need such a script when 'grep' is around ?
|
[solved]
Quote:
Code:
#!/bin/bash |
Without studying your code in detail, it looks like you've re-invented a grep search piped to more. Beyond that, may I humbly suggest a different name for your script than ps, which is already the name of a commonly used program to display a list of processes on the host?
--- rod. |
Quote:
Good idea on the script name - thanks. How about mySearch? That sounds like a spyware program :-P. |
All times are GMT -5. The time now is 08:58 AM. |