Solved

Need shell script to split line into variables

Posted on 2006-07-21
4
668 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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