Solved

Need shell script to split line into variables

Posted on 2006-07-21
4
671 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
[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
4 Comments
 
LVL 35

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 35

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 35

Expert Comment

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

 James
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

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 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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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