Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
262
rated 0 times [  262] [ 0]  / answers: 1 / hits: 189334  / 3 Years ago, sat, november 20, 2021, 2:18:10

When I search for tabs in a file with (e)grep I use the litteral tab (^v + <tab>). I can not utilize as a replacement for tabs in regular expressions. With e.g. sed this expression works very well.



So is there any possibility to use a non-litteral replacement for <tab> and what are the backgrounds for a non working / not interpreted ?


More From » bash

 Answers
3

grep is using regular expressions as defined by POSIX. For whatever reasons POSIX have not defined as tab.



You have several alternatives:




  • tell grep to use the regular expressions as defined by perl (perl has as tab):



    grep -P "	" foo.txt


    the man page warns that this is an "experimental" feature. at least seems to work fine. but more advanced perl regex features may not.


  • use printf to print a tab character for you:



    grep "$(printf '	')" foo.txt

  • use the literal tab character:



    grep "^V<tab>" foo.txt


    that is: type grep ", then press ctrl+v, then press tab, then type " foo.txt. pressing ctrl+v in the terminal causes the next key to be taken verbatim. that means the terminal will insert a tab character instead of triggering some function bound to the tab key.


  • use the ansi c quoting feature of bash:



    grep $'	' foo.txt


    this does not work in all shells.


  • use awk:



    awk '/	/'

  • use sed:



    sed -n '/	/p'



See the wikipedia article about regular expressions for an overview of the defined character classes in POSIX and other systems.


[#44230] Saturday, November 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanda

Total Points: 439
Total Questions: 116
Total Answers: 105

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
eanda questions
Tue, Nov 22, 22, 15:24, 1 Year ago
Sat, Oct 16, 21, 23:48, 3 Years ago
Sun, Jul 3, 22, 09:38, 2 Years ago
Thu, Feb 3, 22, 14:33, 2 Years ago
;