Solved

Need shell script to split line into variables

Posted on 2006-07-21
4
667 Views
Last Modified: 2011-04-14
Hi,
I am needing a shell script that will help automate sending reports via email to our users.  I have everything ready except for one part.

All of the reports that we print has the email address and report name on line 2 of the report.  Like this:
                         mark@gmail.com   IC Trial Balance Report

The email address can be different for each report and the report name different as well.  There will be spaces to the left of the email address then some amount of spaces between the eamil address and the report name.  It is centered on the line.  The report is temporarily stored in a file named /var/tmp/report$$.rpt

What I need, is some code to put in a shell script that will look at line 2 of the report, find the email address and split it into one variable and the report name into another.  That way I can send the email to the email address variable and use the report name as the subject.

Maybe awk or sed?

Thanks,
Mark
0
Comment
Question by:9thTee
  • 3
4 Comments
 
LVL 34

Accepted Solution

by:
James0628 earned 500 total points
ID: 17159579
The report name complicates things since it can have spaces in it.  There are probably simpler answers, but this seems to work.

eaddr=`sed -n "2s/^ *\([^ ][^ ]*\).*/\1/p" /var/tmp/report$$.rpt`

rname=`sed -n "2s/^ *[^ ][^ ]*  *\(.*\) *$/\1/p" /var/tmp/report$$.rpt`


 Basically, the first command looks for any leading spaces, followed by one or more non-spaces (the email address), and puts those non-spaces in the email address variable.  The second command looks for any leading spaces, followed by one or more non-spaces (the email address), followed by one or more spaces, and then puts the rest of the line, excluding any trailing spaces, in the report name variable.

 There are some spaces in those sed strings (two in one place and one in the rest).  If you're going to try those commands, I'd suggest you copy&paste them.

 James
0
 
LVL 34

Expert Comment

by:James0628
ID: 17159589
Hmm.  I've tested the sed commands, but haven't actually tested using them like that to assign values to variables (not running Unix at the moment).  If the values don't come out right, try adding/changing quotes.  Something like:

eaddr="`sed -n '2s/^ *\([^ ][^ ]*\).*/\1/p' /var/tmp/report$$.rpt`"

rname="`sed -n '2s/^ *[^ ][^ ]*  *\(.*\) *$/\1/p' /var/tmp/report$$.rpt`"


 James
0
 

Author Comment

by:9thTee
ID: 17159906
Thanks James, that worked perfectly.

Mark
0
 
LVL 34

Expert Comment

by:James0628
ID: 17160379
You're welcome.  Glad I could help.

 James
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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now