Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Friday, May 22, 2015

Be cautious of using grep --colour=always

I noticed that grep --colour=always can embed additional code (for coloring, for example, ^[[01;31m^[[K    ^[[m^[[K) in your text, which could lead downstream confusion. Here is one such example:
https://stackoverflow.com/questions/18315380/invisible-0131-number-in-bash

$ echo 1 2 3 | grep 2
1 2 3
$ echo 1 2 3 | grep 2 | cat -v
1 2 3
$ echo 1 2 3 | grep --colour=always 2 
1 2 3
$ echo 1 2 3 | grep --colour=always 2 | cat -v
1 ^[[01;31m^[[K2^[[m^[[K 3

See my answer for more detail.

The solution is to use --colour=autoor not use the --colour option at all.


$ echo 1 2 3 | grep --colour=auto 2 
1 2 3
$ echo 1 2 3 | grep --colour=auto 2 | cat -v
1 2 3

Check your .bashrc to see if you set alias grep='grep --colour=always' like I did.

Friday, March 20, 2015

grep -wfF list.txt input.txt

If you are just greping the list from a file, and your list are store in a file, let's say, list.txt, then you can always do grep -wf list.txt input.txt

When list.txt is huge, "-F" will be much faster.

Extracted from https://www.biostars.org/p/134753/#134871

Friday, September 26, 2014

"search and highlight" in linux command line

Is there a way to display a text file in command line, but highlight the matches?

Actually there is a pretty neat tip from jacksonh:
grep --color -E "test|$" yourfile
What we're doing here is matching against the $ pattern and the test pattern, obviously $doesn't have anything to colourize so only the test pattern gets color. The -E just turns on extended regex matching.

Wednesday, August 08, 2012

highlight grep output with color

With --color=auto, grep will highlight the matching portion of the line in color when it outputs to a terminal, while avoiding the use of any potentially-harmful terminal control sequences when standard output goes somewhere else (like a file or pipe). If you like the colorful grep, you can simply export GREP_OPTIONS='--color=auto' in your shell startup script.

grep's default highlight uses a red color. You can change this color by setting the GREP_COLOR environment variable to a different escape sequence fragment. I use export GREP_COLOR='1;32', which produces a bright green.

Ref: http://www.debian-administration.org/articles/460 

Friday, November 05, 2010

Make grep stop after the first match - The macosxhints Forums

Make grep stop after the first match - The macosxhints Forums:

Make grep stop after the first match

Question: I'm trying to use grep to find all the href links in my asp files (except in-page links that start with #).
I want it to show me the href="blahblah" part only - nothing before href or after href's closing quote.
Here's what I have so far. This:
grep -i -r -o 'href="[^#].*"' rentals.asp
gives me this (green=good, blue=bad):
href="<%= webaddr %>/rentalStyle.css" type="text/css"
href="<%= webaddr %>/rentals_s.asp"
href="<%= webaddr %>/rentals_o.asp">rentals
href="<%= webaddr %>/search.asp">Search
Solution:
"I think the question has to do with the 'greediness' of grep when multiple matches occur in the same line (in this case, the double-quote character). To match everything up to the first closing quotes, try:
Code:

grep -i -r -o 'href='[^#][^']*'' rentals.asp

----------------
Similar question to get URL links in html source code like:

< href="ftp://encodeftp.cse.ucsc.edu:21/users/akundaje/rawdata/peaks/jul2010/idr0_02/narrowPeak_blacklistFiltered/wgEncodeBroadHistoneGm12878CtcfStdAlnRep0.bam_VS_wgEncodeBroadHistoneGm12878ControlStdAlnRep0.bam.regionPeak.gz">wgEncodeBroadHistoneGm12878CtcfStdAlnRep0.bam_VS_wgEncodeBroadHistoneGm12878ControlStdAlnRep0.bam.regionPeak.gz< / a >

hint code from commandlinefu :
egrep -o "ftp://[^[:space:]]*.gz"
will get
ftp://encodeftp.cse.ucsc.edu:21/users/akundaje/rawdata/peaks/jul2010/idr0_02/narrowPeak_blacklistFiltered/wgEncodeSydhTfbsK562bTr4UcdAlnRep0.bam_VS_wgEncodeSydhTfbsK562bInputUcdAlnRep1.bam.regionPeak.gz">wgEncodeSydhTfbsK562bTr4UcdAlnRep0.bam_VS_wgEncodeSydhTfbsK562bInputUcdAlnRep1.bam.regionPeak.gz

So, it should be corrected into :
egrep -o "ftp://[^[:space:]^\"]*.gz"

Smart, ha!
- Sent using Google Toolbar"

Tuesday, September 09, 2008

to show multi line around grep result

You can grep multiple lines before or after matching the keywords. Here is a simple tips, that what I discover grep capable of. A is after, B is before.

For example, test.data is like this:

ENSG00000007372 ENSG00000109911 ENSG00000121690
ENSG00000007372 ENSG00000149100 ENSG00000170959
ENSG00000043355 ENSG00000175198 ENSG00000102452
ENSG00000172845 ENSG00000115840 ENSG00000091428
ENSG00000172845 ENSG00000138430 ENSG00000128708
ENSG00000103449 ENSG00000103494 ENSG00000121274
ENSG00000104313 ENSG00000182674 ENSG00000140396
ENSG00000117707 ENSG00000136643 ENSG00000143499
ENSG00000121297 ENSG00000105176 ENSG00000178904

>grep ENSG00000138430  test.data 
ENSG00000172845 ENSG00000115840 ENSG00000091428

while,
>grep ENSG00000138430  test.data -B1 -A3
ENSG00000172845 ENSG00000115840 ENSG00000091428
ENSG00000172845 ENSG00000138430 ENSG00000128708
ENSG00000103449 ENSG00000103494 ENSG00000121274
ENSG00000104313 ENSG00000182674 ENSG00000140396
ENSG00000117707 ENSG00000136643 ENSG00000143499

grep based on multiple words

For example, grep "you" and "me" in the file,

grep "you\|me" filename

You need to put ESCAPE STRING ( \ ) for OR ( | ), else it will treat it as a simbol you want to search instead of regular expression symbol. or

grep -E "you|me" filename

or

egrep "you|me" filename

But, to get line with both "you" and "me", you can use

grep "you" filename | grep "me"

or, 

egrep "you.*me" filename

but this will include those lines like "your lovely meebo", which is not what we want sometime. So, to get the exact words matched, use

egrep "\.*\" filename

For more info about egrep, use "man egrep"

The caret ^ and the dollar sign $ are metacharacters that respectively match the  empty  string  at  the beginning and end of a line.  The symbols \<> respectively match the empty string at the beginning  and end of a word.  The symbol \b matches the empty string at the edge of a word,  and  \B  matches  the  empty string provided it not at the edge of a word.