Selected lines from a file based on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selected lines from a file based on another file
# 1  
Old 11-19-2012
Selected lines from a file based on another file

Hello,

I am using Awk in Ubuntu 12.04

First file: I have a file like this:
Code:
SNP1  1  198.2   
SNP2  1  124.5
SNP3  1  124.4
.
.
.

Second file: I have another file like this:
Code:
SNP2
SNP5
SNP10
.
.
.

I want to create a third file like my first file but keeping ONLY the SNPs that exist in the second file using Awk.
For example like an output:
Code:
SNP2 1 124.5
SNP5 1 433.5
SNP10 1 454.2

Thank you very much in advance.

Last edited by Franklin52; 11-19-2012 at 07:55 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-19-2012
Try (assuming that the first field will always have numbers):
Code:
awk 'FNR==NR{t=$1;sub($1 FS,"");a[t]=$0;next}{$0=$1 FS a[$1]}1' file1 file2

How big will the first file be?

Last edited by elixir_sinari; 11-19-2012 at 06:37 AM..
# 3  
Old 11-19-2012
It is quite big, it has 441,845 rows and 50 columns.

---------- Post updated at 05:31 AM ---------- Previous update was at 05:17 AM ----------

Thank you so much.

There is a problem, my first file has actually 47 columns, how I can print them all?
# 4  
Old 11-19-2012
You may also try

Code:
awk 'NR==FNR{a[$0]=$0;next}{if(a[$1]){print }}' file2 file1

# 5  
Old 11-19-2012
Check my earlier post.
# 6  
Old 11-19-2012
There seems to be a problem because when I count the number of fields, it gives me 1 field instead of 47. When I check the tail of my dataset, I see there are lines with only the first column.

I want to only keep the SNPs in the first file that exist in the second file.

sorry for asking too many questions.
# 7  
Old 11-19-2012
What about
Code:
$ grep -f file2 file1
SNP2 1 124.5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

Deleting selected lines in a file

Hi Guys , I have two files say a1 and a2 having following contents a1 dag wfd a2 dag wfd chire hcm I want to delete only the lines in a2 which are in a1 and final output of a2 should be a2 chire hcm (6 Replies)
Discussion started by: Pradeep_1990
6 Replies

4. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

5. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

6. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

7. Shell Programming and Scripting

Reading selected lines from a param file

Hi all, I have a question for the Gurus. I apologize if this has bee shared before but I couldn't find the link. I am trying to read parameters from an external parameter file. What I m trying to achieve is read selected lines from an external parameter file into the script. for eg my param... (4 Replies)
Discussion started by: maverick1947
4 Replies

8. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

9. Shell Programming and Scripting

Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom. i.e line 1 xyz - not needed line 2 User01 - needed line 3 123 - not needed line 4 Info - needed... (2 Replies)
Discussion started by: rosslm
2 Replies

10. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question