0

I have a text file which looks like this(I am showing few lines of it)

INDEX : 1
snRNA_seq:TTTTGGAGCAGGGAGATGGAAT

miRNA_seq:CTCCTGACTCCAGGTCCTGTGT

target: RNU2-1,RNU2-64P,RNU2-37P,RNU2-61P,RNU2-68P,RNU2-26P,RNU2-57P,RNU2-59P
length: 22
miRNA: hsa-miR-378a-5p*MI0000786
length: 22


mfe: -21.3 kcal/mol

p-value: 0.015469



position  1

target 5'        U             A 3'

                  UUUGGAG CAGGG    

                  GGACCUC GUCCU    

miRNA  3' UGUGUCCU       A     C 5'





INDEX : 10
snRNA_seq:TTGGAGCAGGGAGATGGAAT

miRNA_seq:ATCCTCTCTTCCCTCCTCCCAG

target: RNU2-1,RNU2-64P,RNU2-37P,RNU2-61P,RNU2-68P,RNU2-26P,RNU2-57P,RNU2-59P
length: 20
miRNA: hsa-miR-7111-3p*MI0022962
length: 22


mfe: -24.0 kcal/mol

p-value: 0.001695



position  2

target 5'   U    C        U    A 3'

             GGAG AGGG AGA  GGA    

             CCUC UCCC UCU  CCU    

miRNA  3' GAC    C    U   CU   A 5'

I have list of identifiers namely 1,10,20,30 etc This identifier in the above file is right next to INDEX part. All I want to do is if the identifier matches the above file print that line + next 28 lines basically some thing like this grep -A 28 "INDEX : identifier"

How can i do this in shell??

Regards

  • grep -A 28 "INDEX : identifier" is exactly how you would do this in a shell script. Is this what you are asking? – Digital Trauma Feb 25 '15 at 18:41
  • Yes that is exactly what i want – user3138373 Feb 25 '15 at 19:03
0

If you want to avoid a multi-pass through your data with a fixed subset of IDs given, this is one possible way (for sample indexes 1, 2, 10):

sed -n '/INDEX : \(1\|2\|10\)$/,+28p'
1

You could use grep with -f:

   -f FILE, --file=FILE
          Obtain patterns from FILE, one per line.

but for that you need to format your identifiers file accordingly, e.g. it should read:

INDEX : 1
INDEX : 10
..........
INDEX : 100

instead of:

1,10....100

If you use - as FILE grep will read patterns from stdin so, one way, with awk (and also using grep with -x to match the whole line):

awk -F, '{for (i=1; i<=NF; i++) print "INDEX : "$i}' identifiers | grep -A28 -x -f - file

Unless you want to use your favorite tool to edit identifiers in place and then run:

grep -A28 -x -f identifiers file
  • I undeleted this (and a couple of other As you deleted) because they provide useful information. One of them was also the only answer on the Q so deleting it sets the Q as unanswered. If you really, really want to, you can delete them again but I would much prefer you leave them. – terdon Mar 30 '15 at 12:09
  • Sure, flag away. The thing is that consecutive question deletions raises an automatic flag (to protect against rage quitting I guess). That's why I noticed. As for ignoring, no. Unfortunately. – terdon Mar 30 '15 at 16:51
1

When you have fixed entries for the identifiers you like, you can use

for identifier in 1 10 20 30; do
   grep -A 28 "INDEX : ${identifier}$" Test.txt
done

When you do not have fixed identifiers, you might grep without specifying the identifier:

grep -A 28 "^INDEX : " Test.txt

I use ^ here for matching INDEX lines at the beginning of the line.

  • I have Identifiers like 1,2,3,4,5,10,100 No they are arranged in the input file as 1,10,100,101 Since grep goes line by line I am purposely using this for i in 1 10;do grep -A 28 "INDEX : ${i}" done It gives me 101 102 103 104 etc why so?? Let's say I want 1 and 2 How will I get that?? – user3138373 Feb 25 '15 at 19:09
  • I added an end-of-line character $. Without the dollar grep will find INDEX : 1 as a substring of INDEX : 101 – Walter A Feb 25 '15 at 19:16
  • for identifier in 1 2; do grep -A 28 "INDEX : ${identifier}$" done < Test.txt Does not give INDEX : 2 output Why so??? – user3138373 Feb 25 '15 at 19:21
  • See edited answer: Test.txt should be inside the loop, so each call to grep in the loop will know where to grep. – Walter A Feb 25 '15 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.