grep issue
Hello..
Can some one tell me how to grep "\t" in sting.. Please see the below details for more clarification $ grep "a\t" a.txt $ grep "\tb" a.txt a\tbcdesf $ grep -w "a\t" a.txt $ grep -E -e "a\t" a.txt $ cat a.txt a\tbcdesf $ grep "a\tbcdesf" a.txt $ If I'm grepping "\t" with preceding character, then it's not working.. I tried the above options but still with no Luck..:( |
You will need to use the -P flag:
Code:
grep -P "\t" file.txt |
Option suggested by eosbuddy did not work for me but the -F option did :)
|
bash/ksh have an "ansi-c style" quoting pattern. It expands any backslash characters contained in it, such as "\t", into their literal ansi/ascii equivalent. It's similar in effect to echo's "-e" option.
Code:
grep $'a\t' http://mywiki.wooledge.org/Quotes Another option that should work in most shells is to first hit "ctrl-v", then "tab" (or whatever). The first key combination forces the shell to insert the next keypress as a literal value. Code:
grep 'a<ctrl-v><tab>' Code:
grep 'a\\t' |
David the H.
Your below observations are perfect: Edit: I just realized that you may be referring to a literal "\t" (backslash+t) and not an actual tab. In which case your problem is that you need to escape the backslash to force grep to treat it as literal. bash/ksh have an "ansi-c style" quoting pattern. It expands any backslash characters contained in it, such as "\t", into their literal ansi/ascii equivalent. It's similar in effect to echo's "-e" option. However I was trying: grep $"b\tc" file.txt is not working And I'm doing some run time grep'ing.. so its impossible to escape "\t" This unix box is like hell.. no optiongs like "-P" and "-F" are working here.. I also dont have rooot permission to install the latest grep Only the following options are available :( $ grep -h Usage: grep -hblcnsviw pattern file . . . $ Someone, please give some suggestions, I'm Ok to use other utilities(Like Sed/nawk etc.....) Thnx Jack |
I do not understand this line:
Quote:
|
Will this work?
Code:
grep "a[[:blank:]]b" file.txt |
Hey Grail...
Quote:
I don't have control on that value.. So now I want to pass the select query putput to grep |
@eosbuddy: No it's not working :(
This "grep" is making me crazy |
Also, the following works for me (for a literal \t):
Code:
grep "\\\t" file.txt |
Ok ... I think I get you, but you will need to escape the data somehow for it to work.
So my suggestion would be like so (this is an example you will need to alter to suit): Code:
#!/bin/bash Code:
./script.sh 'a\tb' file |
Grail and eosbuddy,
I agree with your approaches.. But in my case the data are more sensitive and the no. of records are also NOT small, So I'm afraid to do replace "\t" with "\\t"..the column value consist of several special characters as well. For example.. The column value may be like this and there are also more than 40 columns and million number of records "adnle;#\tfefferj" "\tvwlem;" "123\t" " \t*112 " "\t\t\t\s " The values(containing "\t") are not consistent This solaris box also dont have powerful grep utility..Many options are not available in this.. In above thread, I mentioned, which options are available in my unix box |
I am still not sure why this is not so easy?
Code:
$ cat file1 |
Or even this (sed is a line by line utility and should work on a minimalistic install):
Code:
$ cat file1 Code:
$ cat file1 |sed 's/\\t/ /g' |
Grail,
Your approach is again right, but in my scenario it's not simple... See my below scenario Quote:
the $rec is record of a table (captured in External File) table value may be like 1 2 3 4 adlkjlfd;%114 \ttow\p felk&\t* ..... So on... and the grep is not able to capture "\t" and it's failing as there is no match found.. For example: if rec=`echo "1 2 3 4 adlkjlfd;%114 \ttow\p felk&\t*"` then "grep -x "$rec" ${file}_dup" is not working See, If I do replacement in file for "\t" or looping in again, its like repeated traverse through the file. Which is again bad coding because of time complexity Grail, Please let me know, if you have any doubt for my xplanations.. I hope, you will provide me the best solutions :) have a good day... eosbuddy:- This may or may not be <tab> it's literal "\t" Cheers!!! Jack |
@jack_9: the examples I've given in my previous 2 posts are for literal "\t" - particularly the sed example (you can convert the 4 spaces (before the /g) to whatever else you'd like to be in there).
|
Yeah I am still not with you :(
Using your example: If we assume that the following returns "1 2 3 4 adlkjlfd;%114 \ttow\p felk&\t*" Code:
rec=`sed -n "$j{p;q;}" ${file}` Code:
grep -x "${rec//\\/\\\\}" ${file}_dup |
Dunno :( Its not working.....
$ grep -x "${rec//\\/\\\\}" rec1 $ echo $? 1 $ echo "${rec//\\/\\\\}" 1 2 3 4 adlkjlfd;%114 \\ttow\\p felk&\\t* $ echo $rec 1 2 3 4 adlkjlfd;%114 \ttow\p felk&\t* $ cat rec1 1 2 3 4 adlkjlfd;%114 \ttow\p felk&\t* $ |
hmmm ... seems your -x on grep is putting you between a rock and a hard place. I think it is interpreting the \\ to be 2 slashes but when left as a single it thinks of \t
as a tab. I would add that without -x it works just fine. |
Yeah Grail.. you are correct.. the grep(in my unix box) is really poor..
And I did not find any other option except "-x" because I need to match the entire line.. If I wont give "-x" then, the half of line will also match, which wont give me the right result or pattern matching... I think you understood my point.. |
You could put start and end, ie ^$, around the string but you actually have another issue to. It appears that the current solution also has an issue that the final asterisk is not
matched unless escaped. I must say I would wonder who in their right mind would have a database filled with this gibberish?? |
Yeah, grail.. u r correct..
Finger crossed :) Sybase database is also not very reliable in joins for such values(Like Space) So, I wanted my script, should work on file level instead of database level..but in file level also I'm facing this issue.. I was happy with my Linux box and lots of options for grep and was able to do many stuffs.. However this unix box(and Sybase) is not allowing me to free up.. :( |
All times are GMT -5. The time now is 11:00 PM. |