File search for pattern - script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File search for pattern - script
# 1  
Old 02-15-2007
File search for pattern - script

Hi All,

I have two directories as

1) mi/job -> job1.sh, job2.sh, job3.sh
2) mi/sysin -> sysin1, sysin2, sysin3,sysin4

I want to wrrite a script such that it should accept two parameters as directory paths.

$ myscript mi/sysin mi/job.

The script should be able to scan all files in the 2nd parameter mi/job for occurence of files existing in the 1st parameter.
i.e it should scan job1.sh, job2.sh,... for occurences of sysin1, sysin2,...

It should print these occurences.

Also, it should search in the 2nd parameter for the occurence of another script name, except for the script itself. i.e it should check for occurence of sysin1 in sysin2, sysin3,... [all files in the same directory except sysin1[

It should print all these occurences too.

Thanks,
Rahul.
# 2  
Old 02-15-2007
Hi,

Can you show us what you've gotten so far?

Regards
# 3  
Old 02-15-2007
I am not very much clear about what you are looking for. do you want to find the filenames(mi/sysin files) present in your shell programs(mi/job)???
However on a grander basis you could try to start this way.

ls $1 >output
for filename in `cat output`
do

grep $filename $2 >temp.$$

###whatever you want to do here



done
# 4  
Old 02-16-2007
I guess this should work...I'm trying with passing different arguments. Let's see.

Code:
rm -f Output.txt
touch Output.txt

rm -f FinalOutput.txt
touch FinalOutput.txt

ls $1 | cut -d" " -f1 | while read line
do
   find $1 -exec grep -r  - -exclude=$1 $line {} ?; -print >> Output.txt
   b=`grep -i "$line" Output.txt`
   if [ "$b" != "" ]; then
        echo "Value is present in the file"
   else
       #echo "Value is not present in the file"
       echo $line >> FinalOutput.txt
   fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

UNIX Shell Script Help for pattern search

Hi Need help for below coding scenario. I have a file with say 4 lines as below. DEFINE JOB TPT_LOAD_INTO_EMP_DET ( TDPID = @TPT_TDSERVER , USERNAME = @TPT_TDUSER ) ; ( 'DROP TABLE '||@TPT_WRKDB ||'.LOG_'||@TPT_TGT ||' ; ') , SELECT * FROM OPERATOR (FILE_READER) ; ) ; Now I want to... (5 Replies)
Discussion started by: Santanu2015
5 Replies

3. UNIX for Dummies Questions & Answers

Search pattern in File

Hi I want to search pattern in file "S12345678B" which start with S and end with B total length is 10,numeric part 12345678 is variable. e.g S99001229B and S88226901B. please help (2 Replies)
Discussion started by: ashfaque
2 Replies

4. Shell Programming and Scripting

Script to search for a pattern in 30 minutes from a log file

Hello All, I have to write a script which will search for diffrent patterns like "Struck" "Out of Memory" , etc from a log file in Linux box's. Now I will be executing a cron job to find out the results by executing the script once in every 30 minutes. suppose time is 14-04-29:05:31:09 So I... (3 Replies)
Discussion started by: Shubhasis Mathr
3 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

Accepting search pattern to script argument

I have a script in tcsh and I want to have a find option to which I can pass a file search pattern I want using the command: /home/chrisd/tatsh/trunk/hstmy/bin/tcsh/raytrac.tcsh -f=*rc* The set command seems to fail when user does not supply a search pattern (if user just supplies -f, a... (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

perl script to search n place a pattern

hi, i have one file as t1.txt as below hi hello welcome i want perl script to search for the pattern "abcd" in the file. if the pattern doesn't exist, i want to insert that pattern at the end of the same file t1.txt my o/p should be hi hllo welcome abcd thank you (3 Replies)
Discussion started by: roopa
3 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question