Grepping for hex characters - explanation? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Grepping for hex characters - explanation?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-26-2011
mregine mregine is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 22 July 2012, 2:59 AM EDT
Posts: 32
Thanks: 13
Thanked 1 Time in 1 Post
Grepping for hex characters - explanation?

Hello,

Yesterday I was looking for a way to grep for a tab in the shell, and found this solution in several places:

Code:
grep $'[\x09]a[\x09]' # Grep for the letter 'a' between two tabs

I'm fine with most of this, but I don't understand what the $ (dollar sign) before the first quote does. It doesn't work without, but I couldn't find any explanation in the grep man or info pages. The only mention of $ there is as a meta-character that matches the end of a regular expression.

Can someone explain and/or point me to other documentation where I can read it up?

Thanks!
Sponsored Links
    #2  
Old 01-26-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
The $ part actually happens in the shell.


Code:
$ echo $'[\x09]a[\x09]' | hexdump -C
00000000  5b 09 5d 61 5b 09 5d 0a                           |[.]a[.].|
00000008
$

So it's not a special option to grep, it's actually an expression that feeds these characters into grep's expression raw.

That's interesting. I've occasionally used that syntax for making tabs like $'\t' but didn't know you could put whole strings in that.
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
methyl (01-26-2011), mregine (01-26-2011)
Sponsored Links
    #3  
Old 01-26-2011
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 24 October 2014, 3:14 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,469
Thanks: 8
Thanked 548 Times in 525 Posts
My take is the $ is literal, but regex keep morphing under my feet:

Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

I usually just use the tab key or ctrl-V ctrl-whatever, in ' ', or $(echo a|tr 'a' '\027') (but my tr only does octal). But I use ksh, this is a bash-ism:

Code:
$ bash <<!
echo $'\x61'
!
a
$


Last edited by DGPickett; 01-26-2011 at 05:32 PM..
    #4  
Old 01-26-2011
mregine mregine is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 22 July 2012, 2:59 AM EDT
Posts: 32
Thanks: 13
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Corona688 View Post
...an expression that feeds these characters into grep's expression raw.
Can you expand on what the "raw" means? I think I sort of understand what you mean but I'm not sure... I compared the command you gave with the same without the $ and it helps a little, but not completely...?
Sponsored Links
    #5  
Old 01-26-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
Quote:
Originally Posted by mregine View Post
Can you expand on what the "raw" means?
Consider this:


Code:
$ echo -e "hello world\n\n"
hello world


$ echo "hello world\n\n"
hello world\n\n
$

The string is fed into echo as is, leaving \n as two characters, \ and n. When you give echo -e you tell it to understand and translate that sort of escape sequence.

but if you tell echo this:


Code:
$ echo $'hello world\n\n'
hello world


$

...echo doesn't have to translate. The argument is translated before the command is run, by the shell. The characters generated by the escape sequence get fed straight into it.
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
figaro (05-13-2011), mregine (01-27-2011)
Sponsored Links
    #6  
Old 01-27-2011
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 October 2014, 3:51 PM EDT
Location: Amsterdam
Posts: 9,548
Thanks: 285
Thanked 2,426 Times in 2,174 Posts
This construction can be used in bash and ksh93:
man ksh:
Quote:
A single quoted string preceded by an unquoted $ is processed as an ANSI-C string except
for the following:
\0 Causes the remainder of the string to be ignored.
\E Equivalent to the escape character (ascii 033),
\e Equivalent to the escape character (ascii 033),
\cx Expands to the character control-x.
\C[.name.]
Expands to the collating element name.
The Following User Says Thank You to Scrutinizer For This Useful Post:
mregine (01-27-2011)
Sponsored Links
    #7  
Old 01-27-2011
mregine mregine is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 22 July 2012, 2:59 AM EDT
Posts: 32
Thanks: 13
Thanked 1 Time in 1 Post
Thanks to both of you. I've learned something and I even know where to look for more :-)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grepping characters anushree.a Shell Programming and Scripting 3 04-23-2009 05:43 AM
Please help on grepping nua7 Shell Programming and Scripting 3 07-15-2008 09:04 AM
grepping around cbeauty Shell Programming and Scripting 9 08-29-2007 01:56 AM
grepping the first 3 characters from a file rachael UNIX for Dummies Questions & Answers 2 10-15-2001 02:33 PM
grepping the first 3 characters from a file g-e-n-o UNIX for Dummies Questions & Answers 2 10-15-2001 06:11 AM



All times are GMT -4. The time now is 07:54 PM.