Grepping for hex characters - explanation?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping for hex characters - explanation?
# 1  
Old 01-26-2011
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!
# 2  
Old 01-26-2011
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.
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 01-26-2011
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 06:32 PM..
# 4  
Old 01-26-2011
Quote:
Originally Posted by Corona688
...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...?
# 5  
Old 01-26-2011
Quote:
Originally Posted by mregine
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.
These 2 Users Gave Thanks to Corona688 For This Post:
# 6  
Old 01-27-2011
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.
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-27-2011
Thanks to both of you. I've learned something and I even know where to look for more :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

2. UNIX for Dummies Questions & Answers

Replacing hex characters

I have the following file consisting of dates and sample measurements: 05��Oct��2010 1.31�� 06��Oct��2010 1.32�� 07��Oct��2010 1.31�� The hex characters are \xc2\xa0 in sequence. I have tried to remove the characters as follows: sed -i '' -e 's/\xc2\xa0//g' file.dat and as follows... (6 Replies)
Discussion started by: figaro
6 Replies

3. Shell Programming and Scripting

Convert hex values to displayable characters

Hi, I am a bit stuck with displaying characters. I am having values like below in the proper displayable characters. which I would want to print the actual value on the right hand side. I dont want to create an array because I would have to create 255 different values. isnt there another way of... (17 Replies)
Discussion started by: ahmedwaseem2000
17 Replies

4. Shell Programming and Scripting

Replacing hex characters '\x0D' with '\x0D\x0A'

Hello All, I have a requirement where I need to replaced the hex character - '\x0D' with 2 hex characters - 'x0D' & 'x0A' I am trying to use SED - But somehow its not working. Any pointers? Also the hex character '\x0D' can occur anywhere in the line. Can this also be accomplished... (6 Replies)
Discussion started by: paragkalra
6 Replies

5. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

6. Shell Programming and Scripting

Grepping characters

Hi friends, I want your help. I have a flat file. I want a script to search following pattern in it and once it get that pattern it should grep next 7 characters from it and should keep it in output file output.TXT Pattern is RSTD3R0******* In above example, characters in the place of *... (3 Replies)
Discussion started by: anushree.a
3 Replies

7. HP-UX

Hex characters of ascii file

Hi, Whats the command or how do you display the hexadecimal characters of an ascii file. thanks Bud (2 Replies)
Discussion started by: budrito
2 Replies

8. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies

9. UNIX for Dummies Questions & Answers

grepping the first 3 characters from a file

give this a try and let me know if it works grep '^' filename rachael (2 Replies)
Discussion started by: rachael
2 Replies

10. UNIX for Dummies Questions & Answers

grepping the first 3 characters from a file

Hi I was wondering if it's possible to use a command to get the first 3 characters of a line in a text file, I tried grep but it returns the whole line but I am only interested in the first 3 characters. Is this possible with grep or I need any other command? Also is it possible deleting from... (2 Replies)
Discussion started by: g-e-n-o
2 Replies
Login or Register to Ask a Question