LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2011, 09:41 AM   #1
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Rep: Reputation: Disabled
Shell parsing multiple lines


Hi,

I am new to parsing text in shell, I am not the most advanced shell user either, but I know the basics.

I am looking to extract data from the clipboard, extract key items, set them as variables, to place them in a template, then put back in the clipboard.

For example this text would be copied:

EMAIL ACCOUNT


==================================

FIRST NAME: Joe

M.I.:
LAST NAME: Smith

EMAIL: smith@jsmith.com


I want to be able to create variables for First name, middle initial, last name, email, and so on.

so far I have:

cat | pbpaste | grep "^FIRST NAME"

but I can't seem to get any further as grep isn't turning anything up... I have been told to use a combo of sed, grep, and cat...

any and all help is greatly appreciated.
 
Old 09-10-2011, 11:28 AM   #2
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
You may want to consider awk generating the appropriate assignment statements.

If in file extract.awk you have something like:

Code:
/^FIRST NAME:/ { print "firstName="""$3"" }
/^LAST NAME:/ { print "lastName="""$3"" }
/^EMAIL:/ { print "email="""$2"" }
and then in your shell script you can do:

Code:
eval `awk -f extract.awk < _datafile`
echo $firstName
echo $lastName
echo $email
The code will probably have to be more elaborate to serve your need. This is just an illustration of the principle.
 
Old 09-10-2011, 11:52 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Since the clipboard is an X concept, and bash doesn't know about or use X in any way, I doubt that you will succeed in that part. You say you want to 'set them as variables', which implies that there is some further use of the data, but you haven't said anything about what that is. It might be helpful to know how you intend to use those variables in order to create the bset overall solution to the problem. tbrand seems to have captured the basics of the issue.

--- rod.
 
Old 09-10-2011, 02:26 PM   #4
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I appreciate your both of your help!

well basically after setting as variables I want to be able to send an email template.

For example:

Hi $firstname $lastname,

I have received your request for $othervariable...




Obviously more complicated than that, but thats the basics of it. I have to say I will be using it on the Mac -Bash shell, with text expander (snippet program), I would select the text copy it to the clipboard, open an email use the snippet to run my script in the shell, and return the value to the clipboard with the template.
 
Old 09-10-2011, 04:13 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
That seems like way too much user interaction to be of value. If I understand correctly, you are going to manually copy and paste from something existing to some other 'something', where your script will read it and use a template to generate an e-mail passage that you will again manually copy & paste into an e-mail. So, now you have to manually handle everything twice, instead of just once. Why not point your script directly at the original source, and loop through each record within it, creating the template-based output for each record it finds. If it is going to compose e-mail messages, the script can send the e-mail too (and this might be construed as advocacy of a spam engine).

--- rod.
 
Old 09-10-2011, 05:28 PM   #6
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
so I guess Ill have to go into greater detail to make it a bit clearer... It always sounds easier in your head lol...

I will need to copy info from my source- which is a web application (ticketing system)
Again all help is greatly appreciated..
this system has user info in it like I posted above.. Basically each line has some info follow by a : (colon) (First Name: xxxxx)

a mac application (Textexpander) will run a shell script upon invoking a keystroke.

so I will copy the text from the ticketing system, open my email and press a key, and it should run the script and paste the result into the body.

basically this is the type of text I will be copying and want to extract the data from and set the data as variables.



EMAIL ACCESS REQUEST
****
REQUEST ID: 12345
****
ADDED BY: Joe Smith 9/6/2011 12:47:38 PM
****
SUBMITTED BY: Joe Smith 9/6/2011 12:48:18 PM



EMAIL ACCOUNT

==================================
****
FIRST NAME: Mary
****
M.I.:**
****
LAST NAME: Jones
****
EMAIL: maryjones@none.com
****
BUSINESS UNIT: 
****
DEPARTMENT: 
****
PHONE: 
****
LOCATION: xxxxxxx
***
DUE DATE: xxxxxxx
****
COMPUTER TYPE: PC



EMPLOYEES AND REQUIRED ACCESS

** Set mailbox rights per below.

==================================

NAME: Lila Miller

MAILBOX & ARCHIVE: NO ACCESS

SEND OPTIONS: NONE

CALENDAR: READ/WRITE


CONTACTS: NO ACCESS

 
Old 09-10-2011, 07:38 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Code:
cat | pbpaste | grep "^FIRST NAME"
You need to learn how to use the programs you want to use. cat takes the input from file or standard in, and prints them to standard out. Since there's no input for cat here, there's no output, which is in turn not fed into pbpaste, which likely doesn't accept data from standard in (the pipe) anyway.

From online descriptions of pbpaste, it appears to print the content of the clipboard to standard dout, so the use of cat is completely irrelevant. Remove it and it will likely work.

But I wouldn't bother with grep anyway. Loop over the contents and test each line with a case statement. A bit of parameter expansion will strip off the unwanted part of the line before setting the final variable.

Code:
#!/bin/bash

while read line ; do

     case "$line" in

	"FIRST NAME:"*) firstname="${line#*: }" ;;
	"LAST NAME:"*)  lastname="${line#*: }" ;;
	"EMAIL:"*)	emailadd="${line#*: }" ;;

     esac

done < <( pbpaste )

echo "$firstname"
echo "$lastname"
echo "$emailadd"
 
Old 09-10-2011, 08:23 PM   #8
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks so much David the H. for your assistance !

I have tried running that script, but I am getting a syntax error any idea?

Code:
 ./text.sh: line 13: syntax error near unexpected token `<'
./text.sh: line 13: `done < <( pbpaste )'
 
Old 09-10-2011, 08:28 PM   #9
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
also thank you for that link, It is very useful, it looks like that site will be a big help for me!
 
Old 09-11-2011, 05:32 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Process substitution is a bash-specific feature. So are you sure you're using bash? Make sure your first line is #!/bin/bash and not #!/bin/sh, which forces the script to be interpreted according to posix compliance.

If you're unable to use bash for some reason, you'll have to use a workaround, usually a named pipe.
Code:
#!/bin/sh

# Create pipe in TMPDIR, or /tmp if unset. Pipe name is script name+process#.
pipe="${TMPDIR:-/tmp}/${0##*/}.$$.pipe"
mkfifo "$pipe"

# Clean up the pipe whenever the script exits.
trap "rm -f $pipe ; exit" INT TERM EXIT

# Send clipboard content into the pipe, then read from it.
pbpaste >"$pipe" &

while read line ; do

     case "$line" in

          "FIRST NAME:"*)echo "${line#*: }" ;;
          "LAST NAME:"*) echo "${line#*: }" ;;
          "EMAIL:"*)echo "${line#*: }" ;;

     esac

done <"$pipe"
 
Old 09-11-2011, 11:38 AM   #11
felmurfud
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks! It looks like that works, without giving a syntax error. Can I incorporate pbcopy into this? To take the results and copy them into the clipboard?

thank a bunch again!
 
Old 09-11-2011, 12:52 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Certainly you can. But since I'm not familiar with pbcopy, I don't know exactly how it works. It probably just involves reformatting the output into a single echo command and piping it into the program. I'll leave that as an exercise for you to figure out. You should do at least a little of the work yourself.

Have a look at this page of the BashGuide here for details about data redirection. You'll want the "pipes" section in particular.

http://mywiki.wooledge.org/BashGuide/InputAndOutput

Then go back to the beginning and read the whole thing when you have time. It will give you all the scripting basics you need to know.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] help parsing lines into chunks and writing each chunk gnashley Programming 2 09-11-2010 09:49 AM
delete multiple lines from file using shell script mech123 Linux - Newbie 4 06-09-2010 05:04 AM
Need to make multiple lines into one with no extra spacing with shell script. sunksullen Programming 10 05-10-2007 06:20 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 10:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration