?
Solved

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

Posted on 2007-11-13
5
Medium Priority
?
2,849 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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