Solved

Loop through files in Unix, grab substrings in each and email to appropriate users

Posted on 2007-11-13
5
2,819 Views
Last Modified: 2012-08-14
All,

Forgive me for the ugly attempt that I have made, but as you can see, I am no Unix expert. I want to loop through all records in a particular directory /mydir/test and look for all files that are in the following format RPT_TEST_ENROLL_XXX_YYYYMMDD.TXT. I would then like to grab each file, grab the XXX value from each (which may be variable in length) and then send the file to an email address associated with the XXX. Hope that makes sense. Maybe you can tell what I was attempting to do by what I've thrown together.

Thank you very much for your time.

t_date=`date +%Y%m%d`
rpt_file="RPT_TEST_ENROLL_"



 
   txt_files="/mydir/test/RPT_TEST_ENROLL_$$"  
   
    ls -1  *${rpt_file}*${t_date}.TXT > ${txt_files}
   
       
    for txt_files in `cat ${txt_files}`
    do
   
    # example file RPT_TEST_ENROLL_1024_20071113.TXT
    # is 33 characters long without the 1024,
    # which will change with every file
     
# get the length of the file, minus 33 which should give us the length of the uniqu ID
     id_len=length($txt_files)-33
# now grab that ID using a substring
     id_code=substr(${txt_files},19,${id_len})
# now concatenate the unique ID in between DC and @myemail.com to form the email address
     email=DC $id_code @myemail.com
# Now ssend the email with the attachment to the appropriate address
      uuencode $txt_files $txt_files| mailx -s 'New Enrollments' $email

     done      
   if [ ${?} -ne 0 ]
  then
      echo "An error occurred while emailing the file ${txt_files}"
 
   fi

 echo "Successfully emailed the file ${txt_files}."
 
0
Comment
Question by:XOracle
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#!/bin/bash
rpt_file="RPT_TEST_ENROLL_"
for txt_files in *$rpt_file*`date +%Y%m%d`.TXT
    do
   
    # example file RPT_TEST_ENROLL_1024_20071113.TXT
    # is 33 characters long without the 1024,
    # which will change with every file
     
# get the length of the file, minus 33 which should give us the length of the uniqu ID
# now grab that ID using a substring
    id_code=${txt_files:${#RPT_TEST_ENROLL_}:${#txt_files}-${#RPT_TEST_ENROLL_}-12}
# now concatenate the unique ID in between DC and @myemail.com to form the email address
     email=DC$id_code@myemail.com
# Now ssend the email with the attachment to the appropriate address
      uuencode $txt_files $txt_files| mailx -s 'New Enrollments' $email

     done      
    if [ ${?} -ne 0 ]
  then
      echo "An error occurred while emailing the file ${txt_files}"
 
   fi
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#!/bin/bash
rpt_file="RPT_TEST_ENROLL_"
for txt_files in *$rpt_file*`date +%Y%m%d`.TXT
    do
   
    # example file RPT_TEST_ENROLL_1024_20071113.TXT
    # is 33 characters long without the 1024,
    # which will change with every file
     
# get the length of the file, minus 33 which should give us the length of the uniqu ID
# now grab that ID using a substring
    id_code=${txt_files:${#rpt_file}:${#txt_files}-${#rpt_file}-12}
# now concatenate the unique ID in between DC and @myemail.com to form the email address
     email=DC$id_code@myemail.com
# Now ssend the email with the attachment to the appropriate address
      uuencode $txt_files $txt_files| mailx -s 'New Enrollments' $email

     done      
    if [ ${?} -ne 0 ]
  then
      echo "An error occurred while emailing the file ${txt_files}"
 
   fi
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
#!/bin/bash
cd /mydir/test

for file in RPT_TEST_ENROLL_*_$(date +%Y%m%d).TXT
do
  id=$(echo $file | cut -f4 -d_)
  uuencode $file $file | mailx -s "New Enrolments" $id@myemail.com || echo "An error occurred while emailing the file $file"
done
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
Sorry, I read  *${rpt_file}*${t_date}.TXT and didn't notice that there should have been _ before ${t_date}
#!/bin/bash
rpt_file="RPT_TEST_ENROLL_"
for txt_files in *$rpt_file*_`date +%Y%m%d`.TXT
    do
   
    # example file RPT_TEST_ENROLL_1024_20071113.TXT
    # is 33 characters long without the 1024,
    # which will change with every file
     
# get the length of the file, minus 33 which should give us the length of the uniqu ID
# now grab that ID using a substring
    id_code=${txt_files%_*}
# now concatenate the unique ID in between DC and @myemail.com to form the email address
# Now ssend the email with the attachment to the appropriate address
      uuencode $txt_files $txt_files| mailx -s 'New Enrollments' DC${id_code##*_}@myemail.com
0
 

Author Comment

by:XOracle
Comment Utility
Ozo, since you were first to respond, I used your last response, and it worked like a charm. Thank you so much.

Tintin, I will have to attempt yours as well at some point and see what kind of results I get. Thank you as well.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now