LookIntoEast
LookIntoEast

Reputation: 8828

How can I grep lines involving tabs?

I came across some problem with playing with 'grep'; and probably about grepping a tab. I have two files shown as below, both of which are tab-delimited.

FM_DEL_50r.bed
chr1    3392391 3658426 DEL chr1    3392364 3658425 DEL
chr1    4011952 4392064 DEL chr1    4011953 4392062 DEL
chr1    4468526 4665322 DEL chr1    4468523 4665322 DEL

FC_DEL_50r.bed
chr1    2612264 2613324 DEL chr1    2612205 2613007 DEL
chr1    3392391 3658426 DEL chr1    3392391 3658426 DEL
chr1    4011952 4392064 DEL chr1    4011953 4392060 DEL

I hope to grep some lines which appear in both files:

cat FC_DEL_50r.bed |grep `cat FM_DEL_50r.bed |head -2|tail -1|awk '{print $2"\t"$3}'`

However there's error:

grep: 4392064: No such file or directory

I tried cat FM_DEL_50r.bed |head -2|tail -1|awk '{print $2"\t"$3}', it actually works and outputs 4011952 4392064

So maybe we cannot grep format like 'number"\t"number"? thx

edit: how stupid I am. I should use double-quotes..........(I first used single-quote and didn't work...)

cat FC_DEL_50r.bed |grep "`cat FM_DEL_50r.bed |head -2|tail -1|awk '{print $2"\t"$3}'`"

Follow-up questions: I wrote a bash script, based on the questions above:

#!/bin/bash
for((c=1;c<=542;c++))
do
    LINE=`head -$c FM_DEL_50r.bed|tail -1`
        P1=`cat $LINE|awk '{print $1"\\t"$2"\\t"$3}'`
        GREP1=`cat FC_DEL_50r.bed |grep "$P1"`
        X1=`cat $GREP1 |awk '{print $5"\\t"$6"\\t"$7}'`

        P2=`cat $LINE|awk '{print $5"\\t"$6"\\t"$7}'`
        GREP2=`cat MC_DEL_50r.bed |grep "$P2"`
        X2=`cat $GREP2 |awk '{print $5"\\t"$6"\\t"$7}'`

        if [ $X1 -eq $X2 ]
        then 
           echo "$LINE"\t"$X1"
        fi
done

However it produces

cat: chr1: No such file or directory
cat: 27122653: No such file or directory
cat: 27446984: No such file or directory
cat: DEL: No such file or directory
cat: chr1: No such file or directory
cat: 27880115: No such file or directory
cat: 28225069: No such file or directory
cat: DEL: No such file or directory

Seems it splits all columns of one line and cannot recognize them. What's the problem this time? thx

Upvotes: 1

Views: 751

Answers (2)

ring bearer
ring bearer

Reputation: 20803

grep syntax 1-o-1

grep [options] PATTERN [FILE...]

This means grep pattern shown as in PATTERN in file [FILE...]

the pattern txt can not have spaces or tabs (unless quoted) as it will be then evaluated as an argument to grep command. So in your case the command effectively becomes

cat FC_DEL_50r.bed |grep 4011952 4392064

The second part (After pipe ) is basically asking to grep 4011952(pattern) in 4392064(Which needs to be a file)

To fix this, add double quotes around your second expression as :

cat FC_DEL_50r.bed |grep "cat FM_DEL_50r.bed |head -2|tail -1|awk '{print $2"\t"$3}'"

+1 for all the details presented in the question.

Upvotes: 1

MetaEd
MetaEd

Reputation: 3881

The error is occurring because of the backticks. Backticks splice your awk output into the grep argument list. So effectively you are doing:

cat FC_DEL_50r.bed |grep 4011952 4392064

grep, of course, takes 4011952 to be a regex and 4392064 to be a filename, and cannot find the latter.

With the double quotes that you have now added around the argument list, you are getting:

cat FC_DEL_50r.bed |grep "4011952 4392064"

If what you were hoping for was:

cat FC_DEL_50r.bed |grep "4011952\t4392064"

the difficulty is that \t is being interpreted by awk as an escape sequence. To emit a literal \ from awk, you will need to escape it:

{print $2"\\t"$3}

Upvotes: 0

Related Questions