3

Please note I'm not a developer but I have requirements where I'm trying to search an SVN repository for files containing certain table names. I have built this script on the basis of many things I have read here.

printf "table_name, file_name, full_path\n" >> export_list.txt
while read table
do
extract=($(egrep -ilrs $table /path/to/file | egrep -i tag | egrep -v backup))
file_name=$(basename $extract)
printf "$table, $file_name, $extract\n" >> export_list.txt
done < table_list.txt

I am expecting output like this

table_name, file_name, full_path
SOMETABLENAME, SomeFileName, /path/to/file/tag1.0/SomeFileName
SOMETABLENAME, SomeFileName, /path/to/file/tag2.0/SomeFileName
SOMETABLENAME, SomeFileName1, /path/to/file/tag1.0/SomeFileName1
SOMETABLENAME, SomeFileName1, /path/to/file/tag2.0/SomeFileName1

Instead I am only getting one line back for SOMETABLENAME, the first one it finds.

The use of the array came from this post. I have also read the linked post but to be honest I don't really understand it.

If there are other problems with my script (wrong grep etc), or some other better way of doing it, I'd appreciate the help.

EDIT:

table_list.txt is 2000 lines that looks like this

BI_CLAIMS_SUMMARY
BI_CLAIMS_SUMMARY_AUDIT
BI_MEDS_AUDIT
BI_MEDS_PATIENTS
BI_MEDS_SUMMARY

EDIT:

I have made the changes as suggested by Jessie below (thank you), but the output looks very much the same as before I tried to use my (wrong) version of the array.

table_name, file_name, full_path
BI_CLAIMS_SUMMARY, nohup.out, /sas/data/prod/EIP/depot/hiu/data_extract/tags/1.2/source/pl_sql/bi_claims_sum_plan_proc.sql
/sas/data/prod/EIP/depot/hiu/data_extract/tags/1.2/source/pl_sql/bi_claims_sum_proc.sql
/sas/data/prod/EIP/depot/hiu/sql_refresh/tags/1.0/source/10_load_claims_summary_audit_sqlserver.sas
/sas/data/prod/EIP/depot/hiu/sql_refresh/tags/1.0/resources/automation/nohup.out
BI_CLAIMS_SUMMARY_AUDIT, 10_load_claims_summary_audit_sqlserver.sas, /sas/data/prod/EIP/depot/hiu/data_extract/tags/1.2/source/pl_sql/bi_sum_claims_audit_proc.sql
/sas/data/prod/EIP/depot/hiu/data_extract/tags/1.3/source/pl_sql/bi_sum_claims_audit_proc.sql
/sas/data/prod/EIP/depot/hiu/data_extract/tags/1.4/source/pl_sql/bi_sum_claims_audit_proc.sql
/sas/data/prod/EIP/depot/hiu/meds/tags/1.0/source/HIU_RISK_REFRESH_CLAIMS_SUMMARY_AUDIT.sas
/sas/data/prod/EIP/depot/hiu/sql_refresh/tags/1.0/source/10_load_claims_summary_audit_sqlserver.sas

I'm expecting the BI_CLAIMS_SUMMARY to appear on each line. And also the filename nohup.out appears under the file_name, so it is not splitting the output of the grep correctly

0

1 Answer 1

1

First let me say sorry because I'm not exactly sure what your issue is at this time but I believe it is related to the way you are calling the array, and I have the following suggestions:

n=0
printf '%s,%s,%s\n' 'table_name' 'file_name' 'full_path' >> export_list.txt
while read -r table
do
    extract[$n]=$(egrep -ilrs "$table" /path/to/file | egrep -i tag | egrep -v backup)
    file_name=$(basename "${extract[$n]}")
    printf '%s,%s,%s\n' "$table" "$file_name" "${extract[$n]}" >> export_list.txt
    n=$((n+1))
done < table_list.txt
  1. The printf format string should always be separate from the actual content. (printf '%s,%s,%s\n' 'table_name' 'file_name' 'full_path')
  2. When using read you should almost always use the -r argument. This will allow it to read the \ escape character as a literal
  3. always double quote your variables. Because of this and this.
  4. There is a special syntax that must be used when referring to an array/array element. That is ${array[@]} (entire array) or ${array[0]} (first element in the array). When you call it as $array you will always get only the first element.
  5. Because of the above we need a way to ensure we are grabbing the correct array element on each iteration. For this I have added the n variable which will be incremented on each iteration of your loop.

note: There doesn't appear to be any need to use an array at all in this. Unless your code goes on to use extract in other places you could simply set it as a variable and reset it on each iteration of the loop.

Also note: Your question is tagged with "shell-script" but arrays are not specified by the POSIX standard so while most /bin/sh versions support them, there is no guarantee that yours does.

1
  • Thank you jessie. I've made the changes you suggested. I have edited my post to show the output of the changes, but sadly it's still giving me the same result as before I introduced the array
    – SimonB
    Commented Aug 2, 2019 at 6:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.