problem with Regular expression as input in shell script


View Poll Results: Regular expression issue
Problem with regular expression as input 1 100.00%
Accepting special characters as input 0 0%
Voters: 1. This poll is closed

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with Regular expression as input in shell script
# 1  
Old 12-21-2009
problem with Regular expression as input in shell script

Hi,

I have script which will take a string as input and search in a file.

But when I want to search a pattern which has special characters script is ignoring it.

For example: I want to search a pattern "\.tumblr\.com". shell script is removing \ (backslah) and trying to search ".tumblr.com" in a file. File has pattern \.tumblr\.com. How to read a pattern as input as it is(without removing backslah) using shell script.

Script I am using is bourne shell script.

Below is bash version of my system.
/usr/bin/bash --version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)

Please help me to find the solution. I appreciate any ones help.
# 2  
Old 12-21-2009
Try:
Code:
grep '\\\.tumblr\\\.com' infile

# 3  
Old 12-21-2009
Thanks for the reply..

But I cannot grep the pattern directly because I want to take pattern as input see below.

echo "Enter the pattern:"
read 'value'
echo "Checking for the pattern..."
echo "---------------------------"
fgrep "$value" file
echo "---------------------------"

so when I give "\.tumblr\.com" as input, shell is removing backslah(\). I dont want backslah to be removed..

Please help...
# 4  
Old 12-21-2009
Hi, you can use:
Code:
read -r value

# 5  
Old 12-21-2009
Thanks for your help...it works....

But I have another doubt...

how to use regular expression patterns using sed.

For example I have two different patterns
"groups\.google\.com/group\/"
"groups\.google\.com\/group\/"

If I want to search above patterns I need to escape special characters. I can escape it by backslah in sed. Like below commands. I tried them on command line not in script.

sed -n '/groups\\.google\\.com\/group\\\//p' file ( matches "groups\.google\.com/group\/")

sed -n '/groups\\\.google\\\.com\\\/group\\\//p' file ( matches "groups\.google\.com\/group\/" )

But the problem is I can only use one command at once to search any of the patterns. I want both the patterns to be matched by one command.

I am appending below code I am using... Its bit cryptic. I am using -s option to substitute all the special characters with backslah.

------------------------------
echo "Enter the heuristic value"
read heu

reg=`echo "$heu" | sed 's:\.:\\\\.:g; s:[]\[\+\?\-\ \*]:\\\&:g; s:\-[^z9].:\\\\\\\\\\\&:g; s:/:\\\\\\\\\\\/:g;'`

------------------------------

Please help...
# 6  
Old 12-21-2009
You can make your life a lot easier by not using the deprecated backticks and by using $( ... ) instead. E.g.
Code:
$ heu='?a*'
$ reg=$(echo "$heu" | sed 's/[?+*]/\\&/g')
$ echo $reg
\?a\*

# 7  
Old 12-22-2009
You are the best!!!....

I got the output what I want...

However, could you please explain the below queries I have

what is read -r
what is the difference between backquote(`) and $(...) and what is the purpose of $(...) (I guess shell will treat it as variable)

I truly appreciate your help..

cheers!!

---------- Post updated 12-22-09 at 11:42 AM ---------- Previous update was 12-21-09 at 06:49 PM ----------

hi, could you please tell about my above queries..

---------- Post updated at 12:48 PM ---------- Previous update was at 11:42 AM ----------

Could you please answer my above queries...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

incorporating a regular expression statement in a shell script (.sh)

I do have a shell file where I call many unix commands . I would like to add a regular expression step in that shell file, where a text file, say Test.txt has to be openned and all the :'s should be replaced. Basically apply the follwoing regular expression: :%s/://g to that particular text... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

2. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

Regular Expression problem

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (8 Replies)
Discussion started by: hravisankar
8 Replies

5. Shell Programming and Scripting

Problem with regular expression

Witam, mam oto taki ciąg znaków: 8275610268 + 9012383215 =niepotrzebnytextPotrzebuję w bash'u wyciągnąć obie liczby (mają taką samą liczbę cyfr), zapisać je do osobnych zmiennych, ale coś nie idzie, kombinowałem z grepem, ale nie potrafię skleić tego wyrażenia regularnego, no i potem przypisać do... (7 Replies)
Discussion started by: menda90
7 Replies

6. Shell Programming and Scripting

Problem with a regular expression

Hello! I'm working with AWK, and i have this code: /<LOOP_TIME>/,/<\/LOOP_TIME>/ I want that match every everything between <LOOP_TIME> and </LOOP_TIME>, but not if the line have a "#" before the tags. Someone can help me? Thanks! (6 Replies)
Discussion started by: claw82
6 Replies

7. UNIX for Dummies Questions & Answers

Regular Expression Problem

this is how my xyz.log file loooks like :- info ( 816): CORE1116: Sun ONE Web Server 6.1SP5 B08/17/2005 22:09 info ( 817): CORE5076: Using from info ( 817): WEB0100: Loading web module in virtual server at info ( 817): WEB0100: Loading web module in virtual server at perl... (12 Replies)
Discussion started by: chris1234
12 Replies

8. Shell Programming and Scripting

using regular expression an shell script!!

I want to check if the first argument of my shell script starts with a specifiec string? Any Idea?? Thank u (3 Replies)
Discussion started by: andy2000
3 Replies

9. Shell Programming and Scripting

Regular Expression problem

Hi guys I've been trying to write a regular expression. If I'm tryin to validate a sequence of characters as follows... AB1-232-623482-743 43/3 where a) any character after the "AB" can be any alphanumeric character b) the " 43/3" part is optional is there a quick neat way for me... (5 Replies)
Discussion started by: djkane
5 Replies

10. UNIX for Dummies Questions & Answers

Regular Expression Problem

Display all of the lines in a file that contain "Raspberry" followed later in the line by the letter "a" I tried: grep Raspberry*a filename that didn't work Anyone know a solution? (1 Reply)
Discussion started by: netmaster
1 Replies
Login or Register to Ask a Question