LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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: 431
Blog Entries: 32

Rep: Reputation: 3
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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: 228

Rep: Reputation: 53
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: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


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
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 02:16 AM
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:59 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