Perl script to change values in datafiles

Hi all,

Let say I have 2 files, 1 is source file and another is destination file.

Source file contains the following :

Kitten
Dogs
Donkey
Chicken
Turkey

And destination file contains :

Kitten, 0
Dogs, 0
Donkey, 0
Chicken, 0
Turkey, 0
Kitten, 0
 Dogs, 0
 Donkey, 0
 Chicken, 0
 Turkey, 0
Kitten, 0
 Dogs, 0
 Donkey, 0
 Chicken, 0
 Turkey, 0

What I want is to have destination file contains the following after the script is run :

Kitten, 1
 Dogs, 2
 Donkey, 3
 Chicken, 4
 Turkey, 5
Kitten, 1
  Dogs, 2
  Donkey, 3
  Chicken, 4
  Turkey, 5
 Kitten, 1
  Dogs, 2
  Donkey, 3
  Chicken, 4
  Turkey, 5

Can anybody help to create UNIX/Perl script that could read from source file using loop and change values in destination file as above? Pls help :frowning:

Hi

#awk 'NR==FNR{a[$1]=i++;next}{print $1,a[$1]}' FS=, OFS=, i=1 a1 a2  > a2_ && mv -f a2_ a2
# cat a2
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
#

where a1 is your source file and a2 is your destination file.

Guru.

Thanks Guru for your reply :slight_smile:

But in a2 (destination file), after running script i got empty values like this :

Kitten,
Dogs, 
Donkey, 
Chicken,
Turkey,
Kitten,
Dogs,
Donkey,
Chicken,
Turkey,
Kitten,
Dogs,
Donkey,
Chicken,
Turkey,

Please help :(.. Thanks again :smiley:

Hi

Just try this:

#awk 'NR==FNR{a[$1]=i++;next}{print $1,a[$1]}' FS=, OFS=, i=1 a1 a2 
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
#

If yours is sun, try nawk in place of awk.

Guru.

1 Like

Hi Guru, thanks so much! Now the screen print out the values :

flexpm@kl-gauntletz-lucA$ ls
a1          a2          
flexpm@kl-gauntletz-lucA$ nawk 'NR==FNR{a[$1]=i++;next}{print $1,a[$1]}' FS=, OFS=, i=1 a1 a2 

Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
Kitten,1
Dogs,2
Donkey,3
Chicken,4
Turkey,5
flexpm@kl-gauntletz-lucA$ 

Can you help me a little bit more? I want those output to be updated in a2, so when i vi a2 i can see the outputs like above, not print on screen ..
Can you help to modify a bit?

Thanks again :slight_smile:

Hi
Not sure why the first one did not work for you.

I will ask you to try the first one again, it should work for you. If not,

#awk 'NR==FNR{a[$1]=i++;next}{print $1,a[$1]}' FS=, OFS=, i=1 a1 a2  > a2_ 
#mv -f a2_ a2

Guru.

1 Like

This works!! THANK U SO MUCH GURU! I appreciate so much! :slight_smile:

Hi Guru n everybody

I have problem where the script also change the values of other unrelated fields as below :

Source file :

Kitten
Dogs
Donkey
Chicken
Turkey

Destination file :

Kitten, 0
Dogs, 0
Donkey, 0
Chicken, 0
Turkey, 0
Goat, 0
Camel, 0
Wombat, 0
Kitten, 0
Dogs, 0
Donkey, 0
Chicken, 0
Turkey, 0
Goat, 0
Camel, 0
Wombat, 0
Kitten, 0
Dogs, 0
Donkey, 0
Chicken, 0
Turkey, 0
Goat, 0
Camel, 0
Wombat, 0

After the script was runned the destination file become like this :

Destination file :

Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 
Camel, 
Wombat, 
Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 
Camel, 
Wombat, 
Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 
Camel, 
Wombat, 

How do I get Goat, Camel and Wombat to maintain their original values 0 and not being null? Example:

Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 0
Camel, 0
Wombat, 0 
Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 0
Camel, 0
Wombat, 0 
Kitten, 1
Dogs, 2
Donkey, 3
Chicken, 4
Turkey, 5
Goat, 0
Camel, 0
Wombat, 0 

Thanks

Hi

#awk 'NR==FNR{a[$1]=i++;next}{print $1,(a[$1])?a[$1]:$2}' FS=, OFS=, i=1 a1 a2 > a2_
#mv -f a2_ a2

Guru.

Hi Guru,

It's not working :(..
It is still showing null values to
Goat,
Camel,
Wombat,

Hi

Make sure the files a1 and a2 are brought to their previous states before running the commands. Other than that, I dont see any reason.

Guru

1 Like

Thanks Guru..

this is what i did to the script :

echo "Enter filename that you want to modify"
read destfile
nawk 'NR==FNR{a[$1]=i++;next}{print $1, (a[$1])?a[$1]:$2}' FS=," " OFS=," " i=240000 source.txt $destfile > tempfile
mv -f tempfile $destfile

My source file is :

edited, confidential datafile

Actually my destination file (original) was looking like this :

edited, confidential datafile

After running the script above, I'm getting this:

edited, confidential datafile

Which is correct when changing the fields values, but how do I get rid of the "," behind

edited, confidential datafile

etc?

Hi

The sample file you provided is different from your actual file.

Try this:

#awk 'NR==FNR{a[$1]=i++;next}NF>1{print $1,(a[$1])?a[$1]:$2;next}1' FS=, OFS=, i=1 a1 a2 > a2_
#mv -f a2_ a2

Guru.

1 Like

Guru,
That works! :b:
Thank you so much for helping me!:o
U are a real genius! :b::smiley:

Hi Guru,

I need 1 more help.. I promise this is gonna be the last time I bug u :stuck_out_tongue:

My source file :

edited, confidential datafile

My destination file :

edited, confidential datafile

After I run your script

echo "Enter filename that you want to modify"
read destfile
nawk 'NR==FNR{a[$1]=i++;next}NF>1{print $1,(a[$1])?a[$1]:$2;next}1' FS=," " OFS=," " i=1 source.txt $destfile > tempfile
mv -f tempfile $destfile

I found out that I lost the "CST" words at the beginning (line 3 & 4) of the file as below :

edited, confidential datafile

The fields that I want the values to be changed are changed correctly though.
How do I maintain the "CST" word?

Thanks Guru.. you are so kind :slight_smile:

Hi
Your file format keeps changing in every post :slight_smile:

#awk 'NR==FNR{a[$1]=i++;next}NF>1{$2=(a[$1])?a[$1]:$2;}1' FS=, OFS=, i=1 a1 a2 > a2_
#mv -f a2_ a2

Guru.

Hey GURU once again you do a magic for me, that WORKS! Thank you so much!

Hahahah yeah.. now I have to remove all the files because it is work datafile, it supposed to be confidential :stuck_out_tongue: