Select row from file and text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select row from file and text
# 1  
Old 01-30-2014
Select row from file and text

Hi all!
I would like to solve a problem but I have no clue of how do it!I will be grateful if someone could help me!
Briefly I have a big file like this:
HTML Code:
>ENSMUSG00000000204 | ENSMUST00000159637
GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG
>ENSMUSG00000000204 | ENSMUST00000457457
CTGTCCTCTTTCCATGTGCTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000006755 | ENSMUST00000457688
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000037965 | ENSMUST00000068577
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000002323 | ENSMUST00000777544
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
and another file like this (it contains all the ID I want obtain freom the previous file!):
HTML Code:
ENSMUSG00000000204
ENSMUSG00000002323
My desired output should be like that (the selected ID and the relative sequence:

HTML Code:
>ENSMUSG00000000204 | ENSMUST00000159637
GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG
>ENSMUSG00000000204 | ENSMUST00000457457
CTGTCCTCTTTCCATGTGCTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000002323 | ENSMUST00000777544
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
Do you know how can I solve this problem?!
Thank you very much for you support!
# 2  
Old 01-30-2014
Code:
$ cat file1
>ENSMUSG00000000204 | ENSMUST00000159637
GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG
>ENSMUSG00000000204 | ENSMUST00000457457
CTGTCCTCTTTCCATGTGCTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000006755 | ENSMUST00000457688
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000037965 | ENSMUST00000068577
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000002323 | ENSMUST00000777544
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC

Code:
$ cat file2
ENSMUSG00000000204
ENSMUSG00000002323


Code:
$  awk 'FNR==NR{A[">"$1];next}/>/{f=($1 in A)}f' FS="[ |]" file2 file1


Resulting

Code:
>ENSMUSG00000000204 | ENSMUST00000159637
GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG
>ENSMUSG00000000204 | ENSMUST00000457457
CTGTCCTCTTTCCATGTGCTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000002323 | ENSMUST00000777544
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC


Last edited by Akshay Hegde; 01-30-2014 at 10:42 AM.. Reason: --Formatting--
# 3  
Old 01-30-2014
Hello,

Following may help too.

Code:
awk -F" |" 'NR==FNR {a[">"$1];next} ($1 in a){print $0;getline; print $0}' file2 file1


Output will be as follows.

Code:
>ENSMUSG00000000204 | ENSMUST00000159637
GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG
>ENSMUSG00000000204 | ENSMUST00000457457
CTGTCCTCTTTCCATGTGCTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC
>ENSMUSG00000002323 | ENSMUST00000777544
GGCGAGGCTTACGCCATTTTGTGTGAACCTGGCATGCTGGCTTAGGACATGGCCTGATTC

Thanks,
R. Singh
# 4  
Old 01-30-2014
Thanks!;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

2. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

3. UNIX for Dummies Questions & Answers

How to randomly select lines from a text file

I have a text file with 1000 lines, I want to randomly select 200 lines from it and print them as output. How do I go about doing that? Thanks! (7 Replies)
Discussion started by: evelibertine
7 Replies

4. UNIX for Dummies Questions & Answers

Select text between current date and output to new txt file

Hi guys, brand new to this thread and very very new to UNIX...so go easy please! Anyway I have a file that looks like this: >>-------------------------------------------------------------------------- Date/Time/Eng. : 2012-06-22 / 00:26 / DS Reported problem : (SD) ... (5 Replies)
Discussion started by: martin0852
5 Replies

5. Shell Programming and Scripting

mysql how to select a specific row from a table

i have a table records ------------ id | user | time | event 91 admin | 12:00 | hi 92 admin | 11:00 | hi 93 admin | 12:00 | bye 94 admin | 13:00 | bye 95 root | 12:00 | hi 96 root | 12:30 | hi 97 root | 12:56 | hi how could i only select and display only the user and event from... (6 Replies)
Discussion started by: kpddong
6 Replies

6. Shell Programming and Scripting

select always the first row

Hi, I have an output like this: #CMD Output: user1 user2 user3 user4 How can I select ONLY the first row? Thanks (4 Replies)
Discussion started by: iga3725
4 Replies

7. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies

8. Shell Programming and Scripting

random select text file ranamed

i want to need script.. source.txt /home/user1/public_html/test3 /home/user90/public_html/test9 . . . /home/user650/public_html/test000 read source.txt and cd /home/user**/public_html/*** and there is 1.txt, 2txt ~~25.txt and select 6 text files randomly among the... (4 Replies)
Discussion started by: topic32428285
4 Replies

9. UNIX for Dummies Questions & Answers

select distinct row from a file

Hi, buddies out there. I have a text file ( only one column ) which I created using vi editor. The file contains duplicate rows and I would like to select distinct rows, how to go on it using unix command: file content = apple apple orange watermelon apple orange Can it be done... (7 Replies)
Discussion started by: merry susana
7 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question