Sorting data in file based on field in another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting data in file based on field in another file
# 1  
Old 11-08-2011
Sorting data in file based on field in another file

Hi,

I have two files, one of which I would like to sort based on the order of the data in the second. I would like to do this using a simple unix statement.

My two files as follows:

File 1:
12345 1 2 2 2 0 0
12349 0 0 2 2 1 2
12350 1 2 1 2 2 2
.
.
.

File2:
12350
12345
12349
.
.
.

I would like to sort the first based on the order of the field in the second file. My output would look like this:

Output:
12350 1 2 1 2 2 2
12345 1 2 2 2 0 0
12349 0 0 2 2 1 2

Many thanks in advance,
Kathryn
# 2  
Old 11-08-2011
Code:
 sort -nru File1 File2

# 3  
Old 11-09-2011
Thanks Smilesavvy,

Unfortunately this doesn't give me quite what I want. The output it gives is as follows:
Code:
12350 1 2 1 2 2 2
12349 0 0 2 2 1 2
12345 1 2 2 2 0 0

It doesn't seem to be sorting by file2:
Code:
12350
12345
12349

Thanks,
Kathryn

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 11-09-2011 at 04:14 AM.. Reason: code tags
# 4  
Old 11-09-2011
Please start using code tags, thanks.

Not sure if there is an option for some version of sort or grep to achieve this, nevertheless:

Code:
$> cat f1
12345 1 2 2 2 0 0
12349 0 0 2 2 1 2
12350 1 2 1 2 2 2
$> cat f2
12350
12345
12349
$> awk 'NR==FNR{o[FNR]=$1; next} {t[$1]=$0} END{for(x=1; x<=FNR; x++){y=o[x]; print t[y]}}' f2 f1
12350 1 2 1 2 2 2
12345 1 2 2 2 0 0
12349 0 0 2 2 1 2

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

Sorting based on File name

Hi All I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below ABC_TEST_20160103_1012.txt ABC_TEST_20160229_1112.txt ABC_TEST_20160229_1112.txt I have written code as given below to list out... (2 Replies)
Discussion started by: ginrkf
2 Replies

3. Shell Programming and Scripting

Sorting file with CRLF within field, RS=$

OK below is what my sample file looks like. I need to sort by the Primary Key ie: {1:F01SAESVAV0AXXX0466020126} in the first record. Record seperator is $. I tried sort, but it completely messes it up. I am thinking I will need to use something like awk which understands the record seperator... (6 Replies)
Discussion started by: alfredo123
6 Replies

4. Shell Programming and Scripting

Sorting based on the second field

Oracle Enterprise Linux 6 This is my file. Two fields separated by space $ cat testfile.txt MARCH9 MARCH4 MARCH1 MARCH5 MARCH2 MARCH326 MARCH821 MARCH7 MARCH6 MARCH2 $ $ The following numeric sort, based on the first field's 6th character works as expected. $ $ sort -n -k 1.6... (7 Replies)
Discussion started by: John K
7 Replies

5. Shell Programming and Scripting

Sorting file based on name

Hi team, We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed. For example: IN9997 IN9998 IN9999 IN0000... (7 Replies)
Discussion started by: anijan
7 Replies

6. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

7. Shell Programming and Scripting

sorting based on a field

the below is sorted as it is. the fields that i'm interested in are the 4th and 5th field. i want to sort the based on the 4th field. my past attempt to do this was to do something like this: awk '{print $4}'| awk '{print $1":"$2}' datafile | sort | uniq however, if i do that, i lose... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

extract data in a csv file based on a certain field.

I have a csv file that I need to extract some data from depending on another field after reading info from another text file. The text file would say have 592560 in it. The csv file may have some data like so Field 1 Field2 Field3 Field4 Field5 Field6 20009756 1 ... (9 Replies)
Discussion started by: GroveTuckey
9 Replies

9. Shell Programming and Scripting

Sorting file by a field, and then by another field.

Hi all, Sorry the title is a mess, but did not find a better description at the time. So here is my problem: I have an input file: 8:Mass40s -- 0 48:Mass40s -- 0 67:Mass40s -- 0 86:Mass40s -- 0 105:Mass40s -- 0 9:Mass -- 1 49:Mass -- 86... (5 Replies)
Discussion started by: Alexis Duarte
5 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question