Solved

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

Posted on 2007-11-13
5
2,846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20275546
#!/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
ID: 20275563
#!/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
ID: 20275717
#!/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
ID: 20276168
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
ID: 20280546
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

688 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