Solved

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

Posted on 2007-11-13
5
2,821 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now