The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-26-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
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.
  #2 (permalink)  
Old 04-26-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
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!
  #3 (permalink)  
Old 04-26-2004
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
I looked at grep ...

You can use -l (ell) to list the filename that the pattern matches...

You can use -w to Select only those lines containing matches that form whole words.


They are incompatible with each other...

Im out of ideas unless you can redo the tr portion... or change how you search for the pattern.

Maybe if you use grep -f and put all possible combos in a file and use -l ...


Just throwing darts in the dark here though.
  #4 (permalink)  
Old 04-26-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
If you are searching for two words separated by any number of spaces, try...

ls "$path" | xargs grep -i "word *word2"
  #5 (permalink)  
Old 04-26-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
How about:
ls "$path" | xargs awk '{gsub(" ","",$0);print FILENAME $0}' | fgrep -i "$search_arg"
  #6 (permalink)  
Old 04-26-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
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..
  #7 (permalink)  
Old 04-27-2004
djsal djsal is offline
Registered User
  
 

Join Date: Apr 2004
Location: Long Island Ny
Posts: 23
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 06:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0