Losing filename in grep output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Losing filename in grep output
# 1  
Old 04-26-2004
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  
Old 04-26-2004
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  
Old 04-26-2004
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  
Old 04-26-2004
If you are searching for two words separated by any number of spaces, try...

ls "$path" | xargs grep -i "word *word2"
# 5  
Old 04-26-2004
How about:
ls "$path" | xargs awk '{gsub(" ","",$0);print FILENAME $0}' | fgrep -i "$search_arg"
# 6  
Old 04-27-2004
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 01:19 AM..
# 7  
Old 04-27-2004
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"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below. ./test.sh admin_usr.txt Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does. Note: I do not wish to... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Dummies Questions & Answers

Usage of grep '^$1' filename

There is a file name list_filenames.dat, this has all the list of all files I need to encrypt, I did not understand what the following syntax is doing: grep -s "^$1" list_filenames.dat, when I manually run this command it just returns all the lines, what is the usage of this ? can someone... (4 Replies)
Discussion started by: okkadu
4 Replies

3. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

4. Shell Programming and Scripting

grep for pattern in filename

Hey guys, here is my code: #!/bin/bash filter=('ubb' 'um2' 'uuu' 'uvv' 'uw1' 'uw2' 'uwh') let num=`ls -l | grep 'sk' | wc -l` read -a lines <<< `ls -l | grep 'sk' | awk '{print $8}'` let finum=${#fi} for ((i=1;i<=$num;i++)) do for ((c=4;c<6;c++)) ... (2 Replies)
Discussion started by: jkobori
2 Replies

5. Shell Programming and Scripting

Grep and rename the filename

Hi All, Can you please help me. The situation is like this. There are many different file name in this directory. I have to grep all the file that the name start with "PTWO" and rename it to COM with the current date. This is the script that I have done and it hit an... (16 Replies)
Discussion started by: badbunny9316
16 Replies

6. Shell Programming and Scripting

Losing new lines when capturing output to variable

Explain this? $ ls | grep -e "crd\|cs" crd cs $ CLONES=`ls | grep -e "crd\|cs"`;echo $CLONES; crd cs $ CLONES=`ls | grep -e "crd\|cs"`;echo "$CLONES"; crd cs (1 Reply)
Discussion started by: blasto333
1 Replies

7. Shell Programming and Scripting

Redirect grep output to dynamique fileName and subdirectory

Hi all i'm new in KSH, i want to write a script to grep a logs files and redirecting the result into a relative subdirectory/file.txt that must be created near to each log file my begin script is : find ./logs -type f -name "*.log" -exec grep error {} \; how i can perform that modest... (10 Replies)
Discussion started by: rambler
10 Replies

8. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Shell Programming and Scripting

using grep and print filename

Hi, I have a question on bash. Basically I would like to print a file name using bash. I am actually trying to grep a particular character in sequential files. I have alot files such that a.txt, b.txt,c.txt...etc. If I found a certain character, I would print that particular filename. I... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

10. Shell Programming and Scripting

grep string and output filename

Hello, I have over 200 files and some of them have the string like "John price $200". I would like to grep the string. Then output the filename which found the string. I have the following script, but it ONLY output the string echo Please input list file name: read listn for file in `cat... (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question