Friday, 28 November 2014

Impact Analysis using Cygwin

Using Grep and Awk to find a quoted string in code using a partial string

The following script will find all occurrences of a partial string.  Print out the whole string and the file name and line number.

Scripts must be placed in the root directory, otherwise use the full path names.

1) First a script to find the regex matches

#!/bin/bash -f

searchString1='\"usp_.*\"'
searchString2='.usp_'

#                                             Path
grep -iR -e $searchString1 -e $searchString2 "./Main Line">all_usp.txt


2) Optional a script to tidy up the output


#!/bin/bash -f

# remove tabs
tr -d " \t" <all_usp.txt>all_usp_temp.txt

# extract filename and usp name
cat all_usp_temp.txt| awk -F'[:"]' 'match($0,/usp_.*"/) {print $1 "\t" substr($0,RSTART,RLENGTH)}'|awk -F'[:"]' '{print $1 "\t" $2}'|sort -u>all_usp_final.txt

# if required convert to dos
unix2dos all_usp_final.txt