The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed please. jerryboy78 UNIX for Dummies Questions & Answers 3 03-16-2008 02:06 PM
little help needed.. hilofat UNIX for Dummies Questions & Answers 3 02-03-2008 11:48 AM
Help needed akhil1460 UNIX for Dummies Questions & Answers 1 06-18-2006 11:16 PM
Help needed hunter87 UNIX for Dummies Questions & Answers 3 06-02-2006 12:45 AM
Help needed in awk Vikas Sood Shell Programming and Scripting 1 05-22-2006 11:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-09-2008
Drenhead Drenhead is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
Question awk help needed

I am trying to write a script that will parse out the e-mail address of a person from the name of a file in a directory.

Example:

filename is:

/home/myname/first.middle.last@email.com.xls

I want to extract just the email address and mail the file to that address.

I want to send the first.middle.last@email.com.xls file to
first.middle.last@email.com

I tried to use awk for this, but I can't seem to get the syntax right.

#!/bin/sh
curdir="/home/myname"

files="$curdir/*.xls"

echo " SPREADSHEETS TO BE SENT " >> /home/myname/xls.logfile

for filename in $files

do
echo $filename >> /home/myname/xls.logfile
awk ' { print substr( $0, 24, length($0) -4) >> $s } ' $filename
echo $s >> /home/myname/xls.logfile
done

I appreciate all help.
  #2 (permalink)  
Old 10-09-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Try this (run it inside the xls directory):

Code:
for f in *.xls; do
  uuencode "$f" "$f"|mailx -s "Sending you $f" "${f%.xls}"
done
If it's not binary (MS Excel) data:

Code:
for f in *.xls; do
  mailx -s "Sending you $f" "${f%.xls}"<"$f"
done
Edit: And if for whatever reason you want to loop from another dir:

Code:
for f in /dir/with/files/*.xls; do
  f="${f##*/}"
  echo "uuencode "$f" "$f"|mailx -s "Sending you $f" "${f%.xls}""
done

Last edited by radoulov; 10-09-2008 at 05:52 PM..
  #3 (permalink)  
Old 10-09-2008
Drenhead Drenhead is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
That worked! Can you give me a quick explanation of what it is doing? Sorry, I'm pretty new to scripting.



Quote:
Originally Posted by radoulov View Post
Try this (run it inside the xls directory):

Code:
for f in *.xls; do
  uuencode "$f" "$f"|mailx -s "Sending you $f" "${f%.xls}"
done
If it's not binary (MS Excel) data:

Code:
for f in *.xls; do
  mailx -s "Sending you $f" "${f%.xls}"<"$f"
done
Edit: And if for whatever reason you want to loop from another dir:

Code:
for f in /dir/with/files/*.xls; do
  f="${f##*/}"
  echo "uuencode "$f" "$f"|mailx -s "Sending you $f" "${f%.xls}""
done
  #4 (permalink)  
Old 10-09-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by Drenhead View Post
That worked! Can you give me a quick explanation of what it is doing? [...]
I suppose you're asking about the parameter expansion:


Quote:
${variable #pattern} value of variable without the smallest beginning portion that matches
pattern
${variable ##pattern} value of variable without the largest beginning portion that matches
pattern
${variable%pattern} value of variable without the smallest ending portion that matches
pattern
${variable%%pattern} value of variable without the largest ending portion that matches
pattern
This is usually described in your shell man pages under Parameter Expansion.
  #5 (permalink)  
Old 10-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
try this
Code:
 
filename=`basename /home/myname/first.middle.last@email.com.xls`
emailadd=`basename /home/myname/first.middle.last@email.com.xls .xls`
  #6 (permalink)  
Old 10-09-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Drenhead View Post
I am trying to write a script that will parse out the e-mail address of a person from the name of a file in a directory.

Example:

filename is:

/home/myname/first.middle.last@email.com.xls

I want to extract just the email address and mail the file to that address.

I want to send the first.middle.last@email.com.xls file to
first.middle.last@email.com

I tried to use awk for this, but I can't seem to get the syntax right.

You don't need awk; use shell parameter expansion.
Quote:
#!/bin/sh
curdir="/home/myname"

files="$curdir/*.xls"

echo " SPREADSHEETS TO BE SENT " >> /home/myname/xls.logfile

for filename in $files

Don't do that! It will break your script if any filenames contain whitespace. Always use filename expansion directly:

Code:
for filename in "$curdir"/*.xls
do
  base=${filename##*/}
  email=${base%.xls}
done
  #7 (permalink)  
Old 10-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
Quote:
for filename in "$curdir"/*.xls
do
base=${filename##*/}
email=${base%.xls}
done
i think its similar to what i suggested..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0