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 used something like

cat *.txt|grep -w 56|awk '{
if($1 -eq "bla1")
then 
  print the filename (e.g a.txt)

Currently, I try to print some simple echo inside but i could not see any output.

Please help!.Thanks.

-Jason

see your grep man page please

# grep -l "search_stirng"  *.txt

Hi,

Initially, I think of using this command too. But, the motivation is to find the match of number in these files and extract the first field. If the first field is the "search string" it would print the filename.

In other words, I have to get two information. For example:-

Input file (a.txt) has:-

@@@ 10 testingtestingtestingtesting

Initially, i need to grep the 10 in any *.txt and then get the first field. If first field is @@@@, i would print the filename.

If using the above command, I could only get the filename when I need to match only one type of information.

Any suggestion?

Thanks.

Rgrds,
Jason

awk '/10/ && $1=="@@@"{print FILENAME}' *.txt

Hi,

If your search criteria exists as the first field, like as in the example you stated the lin to be searched for is of the form

@@@@ 10 jghdjksghrs

then you may use the following

for var1 in `grep -l 10 *`
do

    num1=\`cut -d" " -f1 $var1 | grep -w @@@@ | wc -l\`
    if [ $\{num1\} -gt 0 ]
    then
            echo $var1
    fi

done

Herein, please replace " " in num1=`cut -d" " -f1 $var1 | grep -w @@@@ | wc -l` with the delimiter you have.

Else if there is no specified delimiter, e.g.

@@@@ 10 dhgjkdsfhfgkjhn
@@@@123154gnfdjkbnf gdn ghk l

then use the following

for var1 in `grep -l 10 *`
do

    num1=\`grep ^@@@@ $var1 | wc -l\`
    if [ $\{num1\} -gt 0 ]
    then
            echo $var1
    fi

done

Do lemme now in case you have any doubt.

take note that if there are files with spaces, var1 will have ambiguous results
If a loop is desired, its better to use while loop and read

# grep -l 10 * | while read line
do
  # do something....  
done