Adding a backslash to users' input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a backslash to users' input
# 8  
Old 04-30-2004
It sounds like you are trying to force egrep to behave like fgrep, so perhaps use fgrep instead?

From man fgrep...
Quote:
fgrep (fast grep) searches files for a character string and prints all
lines that contain that string. fgrep is different from grep(1) and
egrep(1) because it searches for a string, instead of searching for a
pattern that matches an expression. It uses a fast and compact
algorithm.

The characters $, *, [, ^, |, (, ), and \ are interpreted literally by
fgrep, that is, fgrep does not recognize full regular expressions as does
egrep. Since these characters have special meaning to the shell, it is
safest to enclose the entire string in single quotes '...'.
# 9  
Old 04-30-2004
Thanks, Ygor. I do need the functionality that egrep has, such as '|' (search either) and '.*' (any number of characters/spaces), neither of which fgrep provides. But I also need to 'turn off' some of this functionality for common search strings that contain parenthesis and periods.

After my previous post, I realized that in escaping the period, I also disabled the ability to use '.*' in the search. So my 'final product' wasn't so final...here's what I have now, which allows using '.*', but escapes the period if it precedes a number between 0 and 255.

search_arg=`echo "$*" | tr -s ' ' | sed 's/\([()]\)/\\\\\1/g' | sed 's/\(\.[0-255]\)/\\\\\1/g'`
# 10  
Old 04-30-2004
How about:
search_arg=`echo "$*" | tr -s ' ' | sed 's/\([()]\)/\\\\\1/g;s/\(\.[0-255]\)/\\\\\1/g'`
which saves a sed process.


Or...

search_arg=`echo "$*" | sed 's/ */ /g;s/\([()]\)/\\\\\1/g;s/\(\.[0-255]\)/\\\\\1/g'`

which eliminates the tr process too.

Last edited by Perderabo; 04-30-2004 at 10:52 AM..
# 11  
Old 04-30-2004
I'm all for more efficiency, thanks Perderabo!
# 12  
Old 04-30-2004
I've run into a glitch...

While testing my script, I found that many numbers are not getting 'backslashed'! Here's the line in the script that does the backslashing for .0 through .255:

search_arg=`echo "$*" | sed 's/ */ /g;s/\([()]\)/\\\\\1/g;s/\(\.[0-255]\)/\\\\\1/g'`

I put this in a FOR loop and echo'd the results, and here are the numbers that did not get a backslash:
.3
.4
.6
.7
.8
.9
.30
.31
.32
.33
.34
.35
.36
.37
.38
.39
.40
.41
.42
.43
.44
.45
.46
.47
.48
.49
.60 through .99
All other numbers up to .255 received the appropriate backslash.

Any ideas on what's causing this?
# 13  
Old 04-30-2004
[0-255]

is a character in the range 0 through 2 or a 5 or a 5.
# 14  
Old 04-30-2004
Again, thanks for the help.

Here's the correct form:
search_arg=`echo "$*" | tr -s ' ' | sed 's/\([()]\)/\\\\\1/g;s/\(\.[0-9]\{1\}\)/\\\\\1/g'`
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not able to input \ backslash when vi editor

Hi, How do I input \ when I do a vi of my file ? I try to input the \ but it came out as @. Appreciate any help. (4 Replies)
Discussion started by: snowfrost88
4 Replies

2. Shell Programming and Scripting

Adding users from a txt fille

hello i'm making a bash script for adding users from a txt fille i have a basic script that adds users and their password . when you type the users by hand , now i want to upgrade my script with a txt file of users and their password , but i don't know how to start . my txt file looks... (10 Replies)
Discussion started by: Roggy
10 Replies

3. Shell Programming and Scripting

Adding a backslash in front of square brackets with sed

I'm trying to convert this line: to \ with sed. This is what I have so far: sed -e 's/\]*\)\]/\\\\\/' but this still gives me . Any suggestions? (15 Replies)
Discussion started by: lehaste
15 Replies

4. Shell Programming and Scripting

Simple script for adding users

Hi guys, I've a simple linux script (made by my friend), which adds users to the system from userlist file. it also creates user home dir and copies certain files to the directory. To be honest, am a newbie in scripting so am unable to fully understand how the script is working. unfortunately,... (30 Replies)
Discussion started by: vish6251
30 Replies

5. AIX

adding users via smit

I apologize if this is a simple/stupid question. When I add users in smit as root, many(most) of the fields are automatically popluated with some basic default values. Some other admins here have access to create users via sudo, however when they create users (sudo smit users), the user gets... (3 Replies)
Discussion started by: mshilling
3 Replies

6. UNIX for Dummies Questions & Answers

Adding users question

Hello there, I want to add new users to my system, so, being logged in as root I do useradd -m user_name, and the new user is added to the system. The problem is that it has more privileges than I expected. If I do su user_name then I am allowed to do cat /etc/passwd , so it is... (4 Replies)
Discussion started by: help.goes.here
4 Replies

7. Shell Programming and Scripting

Adding delimiter to logged in users

Hi guys! Just was wanting to run a command that would allow me to seperate the currently logged in users. Basically from this format: user1 user2 user3 To: user1|user2|user3 (Note the lack of a pipe at the end, not sure if thats possible) Basically it needs to be in this... (11 Replies)
Discussion started by: crawf
11 Replies

8. Programming

reg adding Users into at.allow and removing from at.allow

Hi , Thanks for your time . I am working on a application , which adds unix user through useradd and deletes user through userdel . both are admin commands . My requirement is i have to add a user into at.allow whenver a unix user is added through my application and the user should be... (4 Replies)
Discussion started by: naren_chella
4 Replies

9. UNIX for Dummies Questions & Answers

Adding users to /etc/group

I'm using SAM to add users on an HP and they're adding fine. But in /etc/group it only lists the group names. It's not adding the users in there. Is there a way to have them put in there without going into SAM and modifying the group and adding them? I guess what I want to happen is when I add... (1 Reply)
Discussion started by: golfhakker
1 Replies

10. Shell Programming and Scripting

Adding users

Anyone have a simple shell script that will prompt and accept screen input for each field that is required in the /etc/passwd file? (3 Replies)
Discussion started by: Relykk
3 Replies
Login or Register to Ask a Question