How to grep B file according to A file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep B file according to A file?
# 1  
Old 07-09-2013
How to grep B file according to A file?

Hi, i have two files: A.txt and B.txt. How can i get C.txt from B.txt according to A.txt?

Here are two examples of A.txt and B.txt:

A.txt
Code:
Contig1
Contig3
ESVER1N
ESVER1M

B.txt
Code:
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT

>Contig2
GATCCCCAAATACCCAAAAGGGTTTTCCCCAAATATAATAT

>Conitg3
GATCAGCATGGGGACCCCTTTAA

>ESVER1L
ATTTATTGCCCCGGGTTTACT

>Contig8
AATCGTACCGGAC

>ESVER1N length=1 description
GATGGGGGCCCTACTTTTAAACC

>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

C.txt
Code:
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT

>Conitg3
GATCAGCATGGGGACCCCTTTAA

>ESVER1N length=1 description
GATGGGGGCCCTACTTTTAAACC

>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

Thanks!!

Last edited by Scott; 07-09-2013 at 08:32 PM.. Reason: Use code tags, please...
# 2  
Old 07-09-2013
Code:
$ grep -A1 -f A.txt B.txt
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT
--
>Contig3
GATCAGCATGGGGACCCCTTTAA
--
>ESVER1N length=1 description
GATGGGGGCCCTACTTTTAAACC
--
>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

This assumes only one line follows the matched item in B.txt.

If your grep doesn't support -A, a similar thing can be done in AWK.
# 3  
Old 07-09-2013
Thanks.
However, there are more than one line follows the matched item in B.txt, and the line numbers are different.

Quote:
Originally Posted by Scott
Code:
$ grep -A1 -f A.txt B.txt
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT
--
>Contig3
GATCAGCATGGGGACCCCTTTAA
--
>ESVER1N length=1 description
GATGGGGGCCCTACTTTTAAACC
--
>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

This assumes only one line follows the matched item in B.txt.

If your grep doesn't support -A, a similar thing can be done in AWK.
# 4  
Old 07-09-2013
That grep was rubbish, anyway!

Code:
$ awk 'NR==FNR {
  A[">"$1]=">"$1
  next
}

A[$1] {
  print
  while( $1 && getline !=0 )
    print
}' A.txt B.txt                       
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT

>Contig3
GATCAGCATGGGGACCCCTTTAA
SOME BLAH
MORE BLAH

>ESVER1N
GATGGGGGCCCTACTTTTAAACC
BLAH
BLAH
BLAH

>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

Use /usr/xpg4/bin/awk or nawk on Solaris.
# 5  
Old 07-09-2013
Thanks,Scott,

It works perfectly.



Quote:
Originally Posted by Scott
That grep was rubbish, anyway!

Code:
$ awk 'NR==FNR {
  A[">"$1]=">"$1
  next
}

A[$1] {
  print
  while( $1 && getline !=0 )
    print
}' A.txt B.txt                       
>Contig1
ATTTTAGACTACATTTTAAGGCCGACT

>Contig3
GATCAGCATGGGGACCCCTTTAA
SOME BLAH
MORE BLAH

>ESVER1N
GATGGGGGCCCTACTTTTAAACC
BLAH
BLAH
BLAH

>ESVER1M
TCCCCAAAGGGGCCCTTTTATAGGGATTCAT

Use /usr/xpg4/bin/awk or nawk on Solaris.
# 6  
Old 07-09-2013
Better
Code:
while ( NF && getline !=0 )

that continues if $1 is 0.
Further little improvements:
A[">"$1]=1 is sufficient for the test A[$1]
and
A[">"$1](no value) for the smart test ($1 in A)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

3. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

4. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

5. Shell Programming and Scripting

Shell Script to grep Job File name and Log File name from crontab -l

Hello, I am new to shell scripting. I need to write a shell script where i can grep the name of file ie. .sh file and log file from crontab -l. #51 18 * * * /home/oracle/refresh/refresh_ug634.sh > /home/oracle/refresh/refresh_ug634.sh.log 2>&1 #40 17 * * * /home/oracle/refresh/refresh_ux634.sh... (1 Reply)
Discussion started by: guptra
1 Replies

6. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

9. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

10. UNIX for Dummies Questions & Answers

Grep in a file for a particular pattern in a particular position witihn the file

Assume I have a file with a lot of data sets like 123 abc 01 456 def 02 789 ghi and I only want to grep all that datasets from my file having the pattern '02' at the postion 9-10 to get only 456 def 02 So I could group the datsets into three files according to the position 9-10, one... (9 Replies)
Discussion started by: ABE2202
9 Replies
Login or Register to Ask a Question