Visit Our UNIX and Linux User Community


Put numbers against the words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put numbers against the words
# 1  
Old 07-19-2011
Put numbers against the words

Hi All,

I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files.

I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using
Code:
sort -u

from the actual .dat files.

This is what I mean:
For example, my 1.dat looks like this:
Code:
apple
orange
tomato
computer
tomato
computer
apple

After I use
Code:
sort -u 1.dat

I get 1.dic like this:
Code:
apple
computer
orange
tomato

I did the same for all 500 files. That is create .dic from .dat

I have another set of files with extension .fil These files contain numbers for the words in stored in .dic Hence, for 1.dic my 1.fil looks like this:

Code:
2.33
0.55
1.43
0.0009983

Now in all, this is the scenario:
I) 1.dat the main file with the complete set of words.
II) 1.dic the file created by
Code:
sort -u

on 1.dat
III) 1.fil the file containing numbers and these numbers correspond to some values of the words stored in 1.dic, this means for every word in 1.dic, there is some value stored in 1.fil

The task which I am trying to accomplish:
I want to look in each .dic file, then get the words line by line one at a time, then get the value corresponding to that word from .fil file and then search the .dat file for the position of that word in the .dat file and store that number against the position in some separate file, lets name it 1.num.

Taking above case here's the output for 1.num:

Code:
2.33
1.43
0.0009983
0.55
0.0009983
0.55
2.33

One can notice that I just looked at the 1.dic got the word, got the corresponding value from 1.fil, then searched the 1.dat and placed that number against the position of that word in 1.num. I have to do this for some 500 files of the same types that is .dic, .dat and .fil and create 500 .num files

This is what I have done so far:
Code:
awk 'NR==FNR{
       A[$1]=NR
       next
     }
     !n{n=NR}
     FNR==1{
       ++m
       close(f)
       f=FILENAME
       sub(/\.dic/,x,f)
       k=f
       f=f".dat"
     }
     {
       getline v<f
       B[A[$1],k]=v
     }
     END{
       for(i=1;i<=n;i++){
         for(j=1;j<=m;j++)printf "%s ",B[i,j]?B[i,j]:0
         print x
       }
     }' *.dat *.dic

I am using Linux with BASH. Any help appreciated.
# 2  
Old 07-19-2011
Using your source files
Code:
perl -e ' 
open my $keys, "<", "1.dic"; 
open my $values, "<", "1.fil";
my %hash;
while(<$keys>){
   my $value=readline($values);
   $hash{$_}=$value;
}
close($keys);
close($values);
open my $data, "<", "1.dat"; 
while (<$data>){
print $hash{$_};
}'
2.33
1.43
0.0009983
0.55
0.0009983
0.55
2.33

Or less formally
Code:
perl -e 'open K,"1.dic"; open V,"1.fil";while(<K>){$v=readline(V);$h{$_}=$v;}open D,"1.dat"; while (<D>){print $h{$_};}'

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 07-19-2011
If I'm not missing something, this should work:

Code:
awk 'END { process( f ) }
FNR == 1 {      
  if ( f ) process( f )
  f = FILENAME
  sub( /\.dic$/, x, f )
  }
getline num < (f ".fil") { map[$0] = num }
func process( f ) {
  while (( getline dat < ( f ".dat" )) > 0 ) {
    print map[dat] > ( f ".num" )
    }
  close( f ".dat" ); close( f ".fil" )
  close( f ".num" ); split( x, map )      
  }' *dic

This User Gave Thanks to radoulov For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #816
Difficulty: Medium
In CSS, E#myid matches an E element with a 'myid' class name.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

2. Shell Programming and Scripting

Put words to fix position in a file

Hi all, There are several lines in my file as a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=44 a=3,b=dene,c=22,d=23422342334,g=vxcvxcv,h=4 a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=678 I take values with this command awk -F '' '{print $1,$2,$3}' a.txt I want to put values to a fix position... (6 Replies)
Discussion started by: bahadiraktan
6 Replies

3. Shell Programming and Scripting

help needed to put instance numbers

Hi All I need help am having a source file as below emp dept class subclass region country division first i need to get line count and i need to divide by 3 it is an parameter passing value number of lines 7 (8 Replies)
Discussion started by: ragu.selvaraj
8 Replies

4. Shell Programming and Scripting

Put double quotes around numbers

Hi, consider a file which has data such as "random text",912345,"54","finish" "random text",9991236745,"9954","finish" I want to replace the numbers that don't have double quotes around them with ones that do; so the output should be "random text","912345","54","finish" "random... (4 Replies)
Discussion started by: Storms
4 Replies

5. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - c 2 b 5 c 5 b 6 a 10 b 16 c 18 a 19 b 21 c 27 a 28 b 33 a 76 a 115 c 199 c 251 a 567 a 1909 (4 Replies)
Discussion started by: maq
4 Replies

7. Shell Programming and Scripting

Difference between words and not between numbers

Hi, Sorry in advance for propably a silly question, but I am a bit lost. On some of the linux job flow I have the following check: if ($file != 1500) then echo ERROR It works ok, all times $file is not equal to 1500 I have the error message. I try to do something similar... (7 Replies)
Discussion started by: essemario
7 Replies

8. Shell Programming and Scripting

Query to print numbers in words

Hi, I have to write a shell script that converts numbers in to words below is what i wrote.My script is not running. ----------------------------------- echo -n "Enter number : " read n len= echo $n | wc -c echo " number in words : " for ( i=1; i<len; i++ ) do num=echo $n... (5 Replies)
Discussion started by: bab123
5 Replies

9. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

10. Web Development

Query to print numbers in words

Hi, If i give a number say "1234" the output of mysql query should be: one thousand and twenty four How to write mysql query for this? With regards Vanitha (5 Replies)
Discussion started by: vanitham
5 Replies

Featured Tech Videos