LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-11-2011, 01:40 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
special character in string variable and grep


Hi, I want to match some filename in some text, but the filenames I have no control of, so "[" can "]" can appear in the filenames:
Code:
#oh, by the way, I am using ^ and $ for matching the exact same line, so I wonder -F option disables that?
	matchname=$(echo "$basename" | sed 's/\[/\\\[/g')
	matchname=$(echo "$matchname" | sed 's/\]/\\\]/g')
	matchname=$(echo "$matchname" | sed 's/\./\\\./g')
	matchname=$(echo "$matchname" | sed 's/\{/\\\{/g')
	matchname=$(echo "$matchname" | sed 's/\}/\\\}/g')
	matchname=$(echo "$matchname" | sed 's/\(/\\\(/g')
	matchname=$(echo "$matchname" | sed 's/\)/\\\)/g')
	matchname=$(echo "$matchname" | sed 's/\+/\\\+/g')
	matchname=$(echo "$matchname" | sed 's/\-/\\\-/g')
	matchname=$(echo "$matchname" | sed 's/\</\\\</g')
	matchname=$(echo "$matchname" | sed 's/\>/\\\>/g')
	matchname=$(echo "$matchname" | sed 's/\&/\\\&/g')
	matchname=$(echo "$matchname" | sed 's/\|/\\\|/g')
	matchname=$(echo "$matchname" | sed 's/\?/\\\?/g')
	matchname=$(echo "$matchname" | sed 's/\*/\\\*/g')
if `cat "/usb/sda1/debian/mnt/sda1/dwhist.log" | grep '^'"$matchname"'$' 1>/dev/null 2>&1`  ; then
....
fi
so do I always have to use sed to addslashes to these variables before I have to grep them? and what other characters have I missed other than "[", "]", "."?
Thanks,
Ted

Last edited by ted_chou12; 04-11-2011 at 01:59 AM. Reason: nothing...?
 
Old 04-11-2011, 02:11 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,433

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
I am not sure I am understanding the issue?
Maybe an alternate example will help, assuming I am on the right track:
Code:
#!/bin/bash

match='[na$ty fi!e n@me.t%d*]'

if grep -q "'$match'" file
then
    echo i found it
else
    echo i did not find it
fi
Throw the string without the quotes into a file some where and change the word 'file' to your filename and run script.
Seems to work just fine for me.
 
Old 04-11-2011, 02:19 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ printf "%q" "[]test[)>&]"
 
Old 04-11-2011, 02:43 AM   #4
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Hi, thanks.
@grail Sorry, not been clear, grep is treating special characters as regex syntax
@kurumi it works nicely.
Thanks

Last edited by ted_chou12; 04-11-2011 at 02:46 AM. Reason: my bad
 
Old 04-11-2011, 07:59 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,433

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
Quote:
grep is treating special characters as regex syntax
Not in the one I provided, but as long as you have a solution
 
  


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
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
block special and character special files s_shenbaga Linux - Newbie 1 04-20-2005 03:49 PM


All times are GMT -5. The time now is 02:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration