Solved

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

Posted on 2007-11-13
5
2,837 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
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
EMC VNX  storage pool  Vs  raid group  question 3 137
change HISTFILE for root user on AIX 3 87
Linux: disable vim auto-comment 7 136
Write an app 10 70
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

860 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