LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2008, 02:19 PM   #1
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 15
insert character into a variable


heya all... i need to insert a backslash into an email address variable (to escape the @ symbol) that a user will provide

basically i have a script that asks the user for an email address for input, which i assign to a variable... lets say test@isp.com is what the user gives me... i need to insert a \ before the @ symbol so it reads test\@isp.com

i was figuring that awk would be the best way to do this, but i dont know how to pass a variable (instead of a file) to awk

any help would be appreciated
 
Old 11-04-2008, 02:34 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Try this:

Code:
#!/bin/bash
ADDR=$1
PARSEDADDR=`echo $ADDR |awk -F@ '{print $1"\\\@"$2}'`
echo PARSED is $PARSEDADDR
You need to escape the "\" with another "\" for awk to understand you literally mean "\@" rather than "escape @".

You then need to escape the first escape for the shell script to understand you're passing an escape to awk. (Fun isn't it? )

Note the character before the echo and at end of that line is a back tick (same key as ~) while the character before and after the brackets on that line are single quote (same key as double quote).
 
Old 11-04-2008, 03:07 PM   #3
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
this is my script file:

echo "Type the full email address:"
read input

addr=$1
parsedaddr=`echo $addr | awk -F@ '{print $1"\\\@"$2}'`
echo parsed is $parsedaddr


here is the output:

Type the full email address:
test@email.com
parsed is \@

so something isn't working (obviously)
 
Old 11-04-2008, 03:15 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
change "addr=$1" to "addr=$input"

In the example I sent you I was assuming passing the email at invocation by typing:

script user@domain

In that the $1 would be the user@domain (first argument passed to script a/k/a first positional parameter).

Instead you are running:

script

In your script you are prompting user then using the "read" directive to assign his response to a variable named "input" so you have to use $input instead of $1.
 
Old 11-04-2008, 03:39 PM   #5
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
thanks, that works... don't know why i didn't see that myself, pretty silly

thx again
 
  


Reply



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
Insert chinese character tahrenyie85 Linux - Newbie 2 01-03-2008 03:59 AM
sed insert newline character jhwilliams Linux - Software 6 06-08-2007 03:13 PM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
How do I insert special character with alt? Meriadoc Linux - Newbie 5 05-17-2006 10:42 AM
Insert a special character with the keyboard? Schreiberling Linux - General 0 03-05-2006 09:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 AM.

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
Open Source Consulting | Domain Registration