?
Solved

Need shell script to split line into variables

Posted on 2006-07-21
4
Medium Priority
?
672 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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