Search for a pattern that exists in the codebase


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern that exists in the codebase
# 1  
Old 05-15-2013
Search for a pattern that exists in the codebase

Hello everyone,

I want to search for a specific pattern in the entire codebase that exists.

Pattern:
I need to find wherever there is a harcoded value "Vxxxx" and "GCxxxx" exists in the perl scripts and shell scripts. (where xxxx will be always number)
I need the script name and the value where the pattern exists in the scripts.

Example:
Script filter_update.pl has a hardcoded "V1234".
Then I need teh script name and the value i.e. filter_update.pl and V1234.

Could someone please advice me a script or a command to find this may be using awk,grep or perl.

Really appreciate your time and efforts.
# 2  
Old 05-15-2013
The output format specification is lacking, but the following might work.

First create a shell script named tester:
Code:
#!/bin/ksh
find "${1:-.}" -type f -exec awk -f findGC+V.awk {} +

(note that if you are using a Solaris/SunOS System, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk in the above statement)
then make it executable:
Code:
chmod +x tester

then put the following awk script in a file named findGC+V.awk:
Code:
# awk script looking for GCxxxx and Vxxxx (where each x is a digit) as words
# in the given input files. 
BEGIN { FS = "[^[:alnum:]]" }
{       for(i = 1; i <= NF; i++)
                if($i ~ /^(GC|V)[0-9]{4}$/)
                        printf("File: %s String: %s Line: %d\n",
                                FILENAME, $i, FNR)
}

Then if you run it using:
Code:
./tester

it will list each matching string and print a line giving the filename, the string, and the line number within that file where the string was found for each regular file found in file hierarch rooted in the current directory.

If you have a directory named Mixed that contains only a file named GC+V1 that contains:
Code:
1 This is a test "GC1234" in quotes
2 GC2345 should match
3 End of line GC3456
4 should not match GC45678 (too many digits)
5 Triple match GC1357, V2468, & "GC3579"
6 short GC123.
7 leading alpha AGC1234
8 trailing alpha V9876x
9 surrounded [GC0987]{V0123}<GC8765>(V7890)<GC1111>"V2222"'GC3333'

and run the script using the command:
Code:
./tester Mixed

the output produced will be:
Code:
File: Mixed/GC+V1 String: GC1234 Line: 1
File: Mixed/GC+V1 String: GC2345 Line: 2
File: Mixed/GC+V1 String: GC3456 Line: 3
File: Mixed/GC+V1 String: GC1357 Line: 5
File: Mixed/GC+V1 String: V2468 Line: 5
File: Mixed/GC+V1 String: GC3579 Line: 5
File: Mixed/GC+V1 String: GC0987 Line: 9
File: Mixed/GC+V1 String: V0123 Line: 9
File: Mixed/GC+V1 String: GC8765 Line: 9
File: Mixed/GC+V1 String: V7890 Line: 9
File: Mixed/GC+V1 String: GC1111 Line: 9
File: Mixed/GC+V1 String: V2222 Line: 9
File: Mixed/GC+V1 String: GC3333 Line: 9

Of course the intent is that you can run the command:
Code:
./tester source_root_directory

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If file pattern exists in directory then continue

he below looks in $dir for any pattern of fileone. As is, it executes but only returns File found if the exact format in the script exsists. Why isn't a pattern of fileone being looked for and if it is in $dir, File found. I think that is what should happen. Thank you :). dir=/path/to if... (5 Replies)
Discussion started by: cmccabe
5 Replies

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

3. Shell Programming and Scripting

sed to add word if pattern exists

Hi, I have a file with data listed below: clm_id double, cnl_id double, cnl_amt double I want output to be clm_id string, cnl_id string, cnl_amt double So replace double by string if the field contains the word _id I tried sed -e 's/_id/& string/' filename The above will not... (2 Replies)
Discussion started by: wahi80
2 Replies

4. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

5. UNIX for Dummies Questions & Answers

Outputting 1 file per row if pattern exists between files

I have many files that can have various amounts of rows. I essentially want to output each row into a new file if a pattern is matched between two files. I have some code that does something similar but I want it to output every single input row from every file into a separate output file; that... (5 Replies)
Discussion started by: verse123
5 Replies

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

7. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies

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