The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed and awk backslash characters potro Shell Programming and Scripting 7 05-19-2008 09:34 AM
reg adding Users into at.allow and removing from at.allow naren_chella High Level Programming 4 04-11-2007 04:56 AM
Adding users to /etc/group golfhakker UNIX for Dummies Questions & Answers 1 03-30-2006 05:27 PM
SUSE 9.1 - Problems with Adding new users plz help sybella1 SuSE 3 07-27-2005 08:31 AM
Adding users Relykk Shell Programming and Scripting 3 03-26-2004 04:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-29-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
Adding a backslash to users' input

Hi,
I need to convert user-input from '(this)' to '\(this\)' before passing it to egrep. I've tried using TR, SED and NAWK to add the backslash, but the most I ever get is a backslash without a '(' or ')'.

Any ideas?

Thanks!
  #2 (permalink)  
Old 04-29-2004
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
why not put the user input into a variable then just add your sting like


egrep \"${user_input}"\ filename_to_search
  #3 (permalink)  
Old 04-29-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
My goal is to insert the backslashes within the user's search string. If I understand your suggestion, the backslashes would surround the entire search string.

Notice that when I converted '(this)' to '\(this\)', the last parenthesis is preceded by a backslash.

The user's search string could easily be something like this as well: '12.1(3)T', so that the parenthesis are completely within the search string.

I hope I didn't misunderstand your suggestion.
  #4 (permalink)  
Old 04-29-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
sed 's/\([()]\)/\\\1/g'
  #5 (permalink)  
Old 04-29-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
Quote:
Originally posted by Perderabo
sed 's/\([()]\)/\\\1/g'
For some reason that I can't determine, this works only from a command line, not from within the script. When run from a script, my search string (which I'm echoing so I can see the conversion) looks like this:
search string: '(this)'
output: \1this\1

From the script:
search_arg=`echo "$*" | tr -s ' '` #remove redundant spaces
search_arg=`echo "$search_arg" | sed 's/\([(.)]\)/\\\1/g'`
echo "$search_arg"
  #6 (permalink)  
Old 04-29-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
This is the trouble with backslashed backslashes...you need more of them each time they get evaluated. And what's with that dot? I didn't have that. I took it back out.

Try:
search_arg=`echo "$search_arg" | sed 's/\([()]\)/\\\\\1/g'`
  #7 (permalink)  
Old 04-29-2004
netguy netguy is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 11
Perfect! Thanks very much!

The dot was my addition...I also want a slash in front of periods, so that they don't act as single-character wildcards.

Here's the final product:
search_arg=`echo "$*" | tr -s ' ' | sed 's/\([(.)]\)/\\\\\1/g'`
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0