Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep find lines that contains " \t"

Tags:

grep

I was requested to make a shell script to check for simple mistakes in files. I wanted to find, for each line if

(regex:) "[ ]\t" ever happens.

The problem is that grep is ignoring the \ and is taking "t" as a literal. I also tried writting the characters themselves in a file and asking grep to read it but it didn't work. Is there a way to find for the regex " \t" in files using any of the usual linux tools (like grep)?

I already tried:

grep -E --ignore-case --line-number --with-filename --file="b" file

(b contains: " ") and also:

grep -E --ignore-case --line-number --with-filename --regexp=" [\t]" file
like image 442
brunoais Avatar asked Mar 31 '12 09:03

brunoais


People also ask

How do you grep a tab?

just use grep "<Ctrl+V><TAB>" , it works (if first time: type grep " then press Ctrl+V key combo, then press TAB key, then type " and hit enter, voilà!)

How do I grep certain lines in a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do you grep all lines containing strings?

The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.

How do you grep the next 4 lines?

To also show you the lines before your matches, you can add -B to your grep. The -B 4 tells grep to also show the 4 lines before the match. Alternatively, to show the log lines that match after the keyword, use the -A parameter. In this example, it will tell grep to also show the 2 lines after the match.


2 Answers

You can use C-style string $'...'

grep $'\t' file.txt

Or sed:

sed -n '/\t/p' file.txt
like image 74
kev Avatar answered Oct 17 '22 00:10

kev


You can use perl regex with --perl-regex option like

 grep --perl-regex "\t"
like image 38
ДМИТРИЙ МАЛИКОВ Avatar answered Oct 16 '22 22:10

ДМИТРИЙ МАЛИКОВ