Solved

Need shell script to split line into variables

Posted on 2006-07-21
4
666 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks James, that worked perfectly.

Mark
0
 
LVL 34

Expert Comment

by:James0628
Comment Utility
You're welcome.  Glad I could help.

 James
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

11 Experts available now in Live!

Get 1:1 Help Now