Aligning numbers in the second file based on first file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Aligning numbers in the second file based on first file
# 1  
Old 09-08-2011
Aligning numbers in the second file based on first file

Hi All,

I have two sets of files with names .dat and .txt. The number of files is really large more than 90000. The files have names like 1.dat, 2.dat,3.dat and so on where as txt files have names like 1.txt, 2.txt, 3.txt and so on

The DAT and TXT files are equal in number. About 90000 each

1.dat files look like this:
Code:
234422 1 .00222
323232 1 3232
32323 1 0.00222
1234 1 1211
2332 1 0.9
233 1 0.883
123 1 45

and its corresponding 1.txt file looks like this:
Code:
123
233
2332
1234
32323
323232
234422

Numbers in the first column of 1.dat are also there in 1.txt in some other order. But the numbers present in 1 column in 1.dat are guaranteed to be found in 1.txt also. This goes with 2.dat, 2.txt and 3.dat, 3.txt and so on.

My requirement is to sort the columns in the 1.dat file so that the numbers in first column align with those in the 1.txt. This is what I mean (suppose my output file is 1.aligned):


Code:
123 1 45
233 1 0.883
2332 1 0.9
1234 1 1211
32323 1 0.00222
323232 1 3232
234422 1 .00222

As you can see, the order of numbers in the first column in 1.align is same as that in 1.txt. Nothing has changed only I have sorted the numbers based on 1.txt and first column of 1.dat and aligned them together in 1.align and I have to do this for all 90000 files.

I am using Linux with BASH.
# 2  
Old 09-08-2011
Code:
awk 'NR==FNR{a[$1]=$0;next} {print a[$1]}' 1.dat 1.txt

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 09-08-2011
Code:
awk '
NR == FNR { a[$1] = $0  }
NR != FNR { print a[$0] }' 1.dat 1.txt

Check and then wrap it in a loop like this:
Code:
for i in $(seq 1 90000); do                                                            
  echo "$i.dat $i.txt"
done

This User Gave Thanks to yazu For This Post:
# 4  
Old 09-08-2011
Thanks.

I was trying to modify the above code and came up with this haven't check it:

Code:
find . -name '*.dat'| xargs  awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' *.txt

I wish I was an owner of a company Smilie

---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------

Oh yes, mine wont work. I disregarded the sequence loop completely. Smilie
# 5  
Old 09-08-2011
Code:
#!/bin/ksh
for txt in *.txt
do
  num=`echo $txt | cut -f1 -d"."`
  awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' $num.dat $num.txt >> $num.sorted
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting a file based on negative and positive numbers

I have a file that is pipe delimited and in Column F they have number values, both positive and negative. I need to take the one file I am starting with and split it into two separate files based on negative and positive numbers. What is the command to do so? And then I need to also transfer... (4 Replies)
Discussion started by: cckaiser15
4 Replies

2. Shell Programming and Scripting

Aligning a file

I have a large text file in following format cat input.txt abc qwert qwer afweferf wdfwefwe ==> kjhjkwdd mnmn ==> jkjkjwekj poiu ==> lklklke tytyutut ==> olkjmnsmn I need to align those lines with the characters " ==>" . I dont want to disturb the lines which dont have "==>". The... (6 Replies)
Discussion started by: ctrld
6 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Splitting a file based on positive and negative numbers

Dear All, I have to split a tab delimited file in two files based on the presence of a positive or negative in column number 9 , for example file: A 1 5 erg + 6766 0.9889 0.9817 9.01882 erg inside upstream B 1 8 erg2 + 6766 0.9889 0.9817 -9.22 erg2 inside... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

5. UNIX for Dummies Questions & Answers

Help with Aligning the content of a txt file

Hello friends Please help me to display the content of a file in specific aligned manner. for ex. the content of the file may be >$TEST WELCOME HI HELLO UNIX SHELL SCRIPTING >$ I want to display the content like . TEST WELCOME HI HELLO ... (18 Replies)
Discussion started by: rajmohan146
18 Replies

6. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

7. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

8. Shell Programming and Scripting

How to sort files based on file name having numbers

Right now there is no unix direct commad that can sort the files base on its name having numbers: We can use the following: In case your file name are like: abc-UP018.zip xyz-UP019.zip ls *|sort -t'-' -k2 (2 Replies)
Discussion started by: asifansari
2 Replies

9. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

10. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies
Login or Register to Ask a Question