matching and extracting info from text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching and extracting info from text files
# 1  
Old 05-04-2011
matching and extracting info from text files

Hi all,

I have two .txt file i.e.

First text file:
Code:
2
4
1
4

Second text file
Code:
2 1.nii.gz
4 334.nii.gz
1 12.nii.gz
4 134.nii.gz

If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with the first column into a separate folder.

Thank you (and no, I this is not a homework question, it's a stupid work around to avoid doing a lot of manual cp and paste with files for many subjects. I am not a programmer).

Last edited by joeyg; 05-04-2011 at 04:25 PM.. Reason: Please wrap program scripts and data samples with CodeTags.
# 2  
Old 05-04-2011
Question

You have the same number (4) twice in the first column. Mistake? Can a number be twice? Or, do the two files line up line-by-line?
# 3  
Old 05-04-2011
Thank you! it's a mistake. First column is the subject ID and they are all unique. Second column, however, does have repetitions.
# 4  
Old 05-04-2011
Does this give you some ideas?

Code:
$ cat sample6.txt
2 1.nii.gz
4 334.nii.gz
1 12.nii.gz
5 134.nii.gz

$ cat sample6in.txt
4
2

$ cat sample6in.txt | sed 's/^/^/' >sample6in.txt2

$ grep -f sample6in.txt2 sample6.txt
2 1.nii.gz
4 334.nii.gz

$ grep -f sample6in.txt2 sample6.txt | awk '{print $2}'
1.nii.gz
334.nii.gz


Last edited by joeyg; 05-04-2011 at 05:04 PM.. Reason: Made a quick correction
# 5  
Old 05-04-2011
Quote:
Originally Posted by vd24

If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with the first column into a separate folder.
So I think you can have duplicated recored in 1st test file, if they are one-to-one related.

Code:
awk '{getline s < "file1"} s==$1' file2

2 1.nii.gz
4 334.nii.gz
1 12.nii.gz
4 134.nii.gz

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print matching fields (if they exist) from two text files

Hi everyone, Given two files (test1 and test2) with the following contents: test1: 80263760,I71 80267369,M44 80274628,L77 80276793,I32 80277390,K05 80277391,I06 80279206,I43 80279859,K37 80279866,K35 80279867,J16 80280346,I14and test2: 80263760,PT18 80279867,PT01I need to do some... (3 Replies)
Discussion started by: gacanepa
3 Replies

2. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

3. Programming

extracting text files

i m unable to extract data from one text files to different text files..i am able to concat two text files in d same file ---------- Post updated at 03:21 PM ---------- Previous update was at 03:16 PM ---------- i want a c program for it (2 Replies)
Discussion started by: asd123
2 Replies

4. Shell Programming and Scripting

Extracting/condensing text from multiple files to multiples files

Hi Everyone, I'm really new to all this so I'm really hoping someone can help. I have a directory with ~1000 lists from which I want to extract lines from and write to new files. For simplicity lets say they are shopping lists and I want to write out the lines corresponding to apples to a new... (2 Replies)
Discussion started by: born2phase
2 Replies

5. Shell Programming and Scripting

Extracting anchor text and its URL from HTML files in BASH

Hi All, I have some HTML files and my requirement is to extract all the anchor text words from the HTML files along with their URLs and store the result in a separate text file separated by space. For example, <a href="/kid/stay_healthy/">Staying Healthy</a> which has /kid/stay_healthy/ as... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Renaming files by matching info from a separate file

Hi All, I could use a bit of help with this as I'm at a loss. I have a number of files all named accordingly: I have a separate text file that is as follows: What I want to do is end up with: I don't know how to do this, though I'm certain it can be done, and would rather learn how... (14 Replies)
Discussion started by: Demosthenes
14 Replies

7. Shell Programming and Scripting

matching names in 2 text files

I have 2 text files like ________________________________ Company Name:yada yada ADDRESS:some where, CITY,STATE CONTACT PEOPLE:first_name1.last_name1,first_name2.last_name2,first_name3.last_name3 LEAD:first_name.last_name ________________________________ & Data file2 ... (1 Reply)
Discussion started by: rider29
1 Replies

8. UNIX for Advanced & Expert Users

Extracting the required text from log files

It would be highly appreciable if any one helps me in this. I am trying to get it done through Java but I love unix and believe it can be done within minutes with couple of lines. The input log file is a text file contains multiple entries seperated by a blank line. Each seperated entry... (7 Replies)
Discussion started by: hareeshram
7 Replies

9. Shell Programming and Scripting

Extracting information from Config files /text processing

Hello All, This is my first post on this forums, which I consider one of the best of its kind. The reason for my post is that I want to export some information form Nagios configuration files to a DB. I know that there are other tools available to do this, like NDO, monarch, etc... But I want to... (3 Replies)
Discussion started by: oconmx
3 Replies

10. UNIX for Dummies Questions & Answers

Extracting Info

i have a file that contain lines like this 9.4.7.8.5.7.9.0.5.7.1.2.msisdn.sub.cs. 1 IN CNAME SDP01.cs. there are about 50,000 lines like this in the files i want to the extract the digits from the above line like:- 947857905712 OS Solaris9 (3 Replies)
Discussion started by: muneebr
3 Replies
Login or Register to Ask a Question