![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to grep / zgrep to output ONLY the matching filename and line number? | vvaidyan | UNIX for Dummies Questions & Answers | 3 | 03-12-2008 05:33 PM |
| using grep and print filename | ahjiefreak | Shell Programming and Scripting | 5 | 01-10-2008 11:47 AM |
| grep string and output filename | happyv | Shell Programming and Scripting | 3 | 11-20-2007 12:16 AM |
| Get Filename and Line Number using grep | ceemh3 | UNIX for Dummies Questions & Answers | 4 | 09-17-2007 10:35 AM |
| how can i add/modify filename after output? | happyv | Shell Programming and Scripting | 3 | 10-03-2006 05:25 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Losing filename in grep output
I have the following line in a script that searches files in several directories and shows the search results on the screen.
ls "$path" | xargs cat | tr -s " " | fgrep -i "$search_arg" But, because I'm also using CAT and TR, the output from the search does not display the name of the file that the results came from. I need to show the file name, either on the same line of ouput, or preceding it. I can get the filename if I remove CAT and TR, but I need to use TR to strip redundant blank spaces between words that would otherwise cause the search to fail (unless my search string also contained the same number of spaces, which wouldn't be possible to predict). Anyone know how I can get the file name, or if there's a search command that ignores redundant spaces? We use the Korn shell. |
|
||||
|
Driver,
Thanks for the response. I should've mentioned that I'm using a circumvention for this problem, which happens to be very similar to what you've shown. But the problem I have with this method is that it's much *MUCH* slower than using XARGS. Since I have thousands of files to search, I'm hoping that there's a solution that still allows me to use XARGS. Again, this is probably something I should've mentioned in my post, but thanks for your help! |
|
||||
|
Perderabo,
The example you gave removes all spaces from the input line, while I need to remove only redundant spaces. But you did give me a good push in the right direction. Here's what I came up with: ls "$path" | xargs nawk '{print "("FILENAME") "$0}' | tr -s " " | fgrep -i "$search_arg" Thank you, and everyone who contributed! Last edited by netguy; 04-27-2004 at 12:19 AM.. |
|
||||
|
Just another way to do it....
------------------------------------- for list in `ls -1t $path` do if grep $search_arg > /dev/null then echo "${path}/${list}" >> yourfile.txt fi done ------------------------------------- This will output filenames that contain the string you are looking for into "yourfile.txt" |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|