using grep and print filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using grep and print filename
# 1  
Old 01-10-2008
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

Code:
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
# 2  
Old 01-10-2008
see your grep man page please
Code:
# grep -l "search_stirng"  *.txt

# 3  
Old 01-10-2008
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
# 4  
Old 01-10-2008
Code:
awk '/10/ && $1=="@@@"{print FILENAME}' *.txt

# 5  
Old 01-10-2008
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.
# 6  
Old 01-10-2008
Quote:
Originally Posted by sam_roy
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
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
Code:
# grep -l 10 * | while read line
do
  # do something....  
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print/cut/grep/sed/ date yyyymmdd on the filename only.

I have this filename "RBD_EXTRACT_a3468_d20131118.tar.gz" and I would like print out the "yyyymmdd" only. I use this command below, but if different command like cut or print....etc. Thanks ls RBD_EXTRACT* | sed 's/.*\(........\).tar.gz$/\1/' > test.txt (9 Replies)
Discussion started by: dotran
9 Replies

2. 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

3. Shell Programming and Scripting

Print filename and last line using awk

Hi, using awk command I want to print filenames and the last line of each file, in a single command line statement. I want to use 'awk', because I want to add more functionality to this logic later. I tried the following on *.sh files in the current directory find . -type f -name "*.sh"... (26 Replies)
Discussion started by: ysrini
26 Replies

4. Shell Programming and Scripting

Print filename with awk

I can got the filename with this script. it's only show "-" in result. cut -d , -f7 CSV_d.* | awk 'OFS=":"{print FILENAME,substr($1,1,8),substr($1,9,2),substr($1,11,2),substr($1,13,2)}' | sort |uniq (2 Replies)
Discussion started by: before4
2 Replies

5. UNIX for Dummies Questions & Answers

grep pipe filename print issue

uname -a SunOS mypc 5.10 Generic_141414-07 sun4v sparc SUNW,SPARC-Enterprise-T2000 uname -a SunOS mypc 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220 find . -name "*.cer" -exec keytool -v -list -printcert -file {} \; | grep -i "Aug 03" Valid from: Mon Jan 29 00:00:00 GMT... (16 Replies)
Discussion started by: shifahim
16 Replies

6. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

7. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies

8. Shell Programming and Scripting

print the filename

#!/bin/ksh for files in `ls *.gz` do gunzip -c $files | awk -v s=$files -F\" '{print s","$6}' done I have tried FILENAME parameter but it did not work please help. (1 Reply)
Discussion started by: harshakirans
1 Replies

9. AIX

split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely User and Status. for eg. the output should like below: User Status ---- ------ a0crk_user A0-B0123 (3 Replies)
Discussion started by: mbak
3 Replies

10. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies
Login or Register to Ask a Question