LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-27-2012, 05:50 AM   #1
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Rep: Reputation: Disabled
Question 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..
 
Old 02-27-2012, 06:38 AM   #2
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
You will need to use the -P flag:

Code:
 grep -P "\t" file.txt
 
Old 02-27-2012, 06:52 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Option suggested by eosbuddy did not work for me but the -F option did
 
Old 02-27-2012, 09:38 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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'
See this link or the bash man page "quoting" section for more on ansi-c quoting.

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>'
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.

Code:
grep 'a\\t'
grep "a\\\t"
Notice that you may need a different number of backslash escapes when using single quotes and double quotes. This is due to the way bash handles backslashes inside double-quotes. Again, see the man page for details.

Last edited by David the H.; 02-27-2012 at 09:46 AM. Reason: as stated
 
Old 03-05-2012, 06:08 AM   #5
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
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

Last edited by jack_9; 03-05-2012 at 06:10 AM.
 
Old 03-05-2012, 06:26 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I do not understand this line:
Quote:
And I'm doing some run time grep'ing.. so its impossible to escape "\t"
 
Old 03-05-2012, 06:45 AM   #7
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
Will this work?
Code:
grep "a[[:blank:]]b" file.txt
 
Old 03-05-2012, 06:52 AM   #8
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Hey Grail...

Quote:
And I'm doing some run time grep'ing.. so its impossible to escape "\t"
The requirement is like: Some data are inserting into a table... the column value contains the "xxx\txxx" or "\t" is in random fashion..

I don't have control on that value.. So now I want to pass the select query putput to grep
 
Old 03-05-2012, 07:45 AM   #9
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
@eosbuddy: No it's not working

This "grep" is making me crazy
 
Old 03-05-2012, 07:51 AM   #10
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
Also, the following works for me (for a literal \t):
Code:
grep "\\\t" file.txt
Regex's are gentle souls and need a lot of patience from us mere mortals ;-)

Last edited by eosbuddy; 03-05-2012 at 07:52 AM.
 
Old 03-05-2012, 08:19 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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

x="$1"

grep "${x//\\/\\\\}" $2
And to run:
Code:
./script.sh 'a\tb' file
Obviously you will get the data by other means but this should help.
 
Old 03-05-2012, 08:59 AM   #12
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
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

Last edited by jack_9; 03-05-2012 at 09:03 AM.
 
Old 03-05-2012, 10:46 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I am still not sure why this is not so easy?
Code:
$ cat file1
"adnle;#\tfefferj"
"\tvwlem;"
"123\t"
" \t*112 "
"\t\t\t\s "
$ cat script.sh
#!/bin/bash

while read -r i
do
    echo "${i//\\/\\\\}"
done<file1
$ ./script.sh
"adnle;#\\tfefferj"
"\\tvwlem;"
"123\\t"
" \\t*112 "
"\\t\\t\\t\\s "
 
Old 03-05-2012, 11:43 AM   #14
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
Or even this (sed is a line by line utility and should work on a minimalistic install):
Code:
$ cat file1
"adnle;#\tfefferj"
"\tvwlem;"
"123\t"
" \t*112 "
"\t\t\t\s "
and then do a
Code:
$ cat file1 |sed 's/\\t/    /g'

"adnle;#    fefferj"
"    vwlem;"
"123    "
"     *112 "
"            \s "
 
Old 03-06-2012, 02:09 AM   #15
jack_9
LQ Newbie
 
Registered: May 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Grail,

Your approach is again right, but in my scenario it's not simple... See my below scenario
Quote:
for ((j=1; j<=$r_cnt; j+=1))
do
rec=`sed -n "$j{p;q;}" ${file}`
grep -x "$rec" ${file}_dup

done
In above snippet;

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
grep issue alape Linux - General 2 01-28-2012 11:49 AM
[SOLVED] tail and grep issue Wim Sturkenboom Linux - General 6 02-06-2008 07:19 AM
grep issue with SuSE 9 vm_devadas SUSE / openSUSE 6 01-24-2007 09:48 AM
Grep parsing issue selfsck Linux - General 3 07-11-2004 08:38 PM
grep -v issue lleemon Linux - General 7 06-16-2003 03:50 PM


All times are GMT -5. The time now is 12:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration