|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
This construction can be used in bash and ksh93:
man ksh: Quote:
|
| The Following User Says Thank You to Scrutinizer For This Useful Post: | ||
mregine (01-27-2011) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Thanks to both of you. I've learned something and I even know where to look for more :-)
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|