Print the grepped string alongwith the filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print the grepped string alongwith the filename
# 1  
Old 02-07-2013
[Solved] Print the grepped string alongwith the filename

We have C shell and we are executing the below script:
Code:
#!/bin/csh -f

if ($#argv != 2) then
  echo "Usage $0 DirecotryPath Inputfilename"
  exit 1
endif

set dir=$1
set fname=$2
echo $dir

foreach line ( `cat $fname` )

echo \ ========================================  >> OBSELETE.lst
echo \ String "$line" found in the below files   >> OBSELETE.lst
echo \ ======================================== \ >>OBSELETE.lst
echo -n             >> OBSELETE.lst
`echo -n grep -ilr $line $dir`  >> OBSELETE1.lst
end

We are passing two parameters to the above shell - one is the base directory and the other is the filename from which all the lines are to be grepped.
The above script serves our purpose, but it is printing too many unnecessary lines which are not found.
Our requirement is that the grep should print the grepped string alongwith the filename.
E.g.:
This is the output I require:
/test/abc/def.txt |string
where /test/abc/def.txt is the filename and string is the grepped string.
# 2  
Old 02-07-2013
Well, not sure where to start... why do you echo -n the grep command? Use on its own and redirect to your output file. Why use echo -n at all? Do you need all output in one single line?

Using the correct options to grep will seriously help:
Code:
-E use extended regexes
-i ignore char case
-r scan directories recursively
-o print matched string

So, if I run
Code:
$ dir=.
$ line="ABC|awk"              # extended regex: match ABC -OR- awk; with -i: either case
$ grep -Eior $line $dir

it will happily output
Code:
./awk/tranposehugefile:ABC
./awk/separateHtmlMail:abc
./awk/tranposehugefile:awk
./awk/merge2:awk
.
.
.
./awk/lowertriangmatrix:awk
./safe/solutions~:awk

which I guess is what you want (don't know if you need regexes; remove if need be) . Of course you're free to replace the : by | with e.g. sed...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-07-2013
Thanks .. This works.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

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

4. Solaris

sed print range alongwith line numbers.

hi working with sed in a shell script using sed to print a range of lines from a given file for example to print lines 12-24 from a file sed 12,24p <filename> however i need to print the line numbers, alongwith the actual lines would this be possible at all? Thanks (1 Reply)
Discussion started by: xinuuser
1 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

Print only Filename based on given String on file name

Hi, I am new to this unix world. Any ways, I would like to write a shell script that can print the file name. Ex : directory will have 5 files with different name.No matter what are contents are. Now I need to find the file which will have particular name (sub string ).Please do not... (5 Replies)
Discussion started by: akb2010
5 Replies

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

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

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

10. 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
Login or Register to Ask a Question