Perl script to change values in datafiles


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to change values in datafiles
# 1  
Old 08-07-2010
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 :
Code:
Kitten
Dogs
Donkey
Chicken
Turkey

And destination file contains :
Code:
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 :
Code:
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 Smilie

Last edited by Scott; 08-08-2010 at 08:18 AM.. Reason: Code tags
# 2  
Old 08-07-2010
Hi
Code:
#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.
# 3  
Old 08-08-2010
Thanks Guru for your reply :-)

But in a2 (destination file), after running script i got empty values like this :
Code:
Kitten,
Dogs, 
Donkey, 
Chicken,
Turkey,
Kitten,
Dogs,
Donkey,
Chicken,
Turkey,
Kitten,
Dogs,
Donkey,
Chicken,
Turkey,

Please help Smilie.. Thanks again Smilie

Last edited by Scott; 08-08-2010 at 08:18 AM.. Reason: Code tags
# 4  
Old 08-08-2010
Hi

Just try this:
Code:
#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.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 08-08-2010
Hi Guru, thanks so much! Now the screen print out the values :

Code:
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 :-)

Last edited by Scott; 08-08-2010 at 08:19 AM.. Reason: Code tags
# 6  
Old 08-08-2010
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,

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

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 7  
Old 08-08-2010
Quote:
Code:
#awk 'NR==FNR{a[$1]=i++;next}{print $1,a[$1]}' FS=, OFS=, i=1 a1 a2 > a2_
#mv -f a2_ a2
This works!! THANK U SO MUCH GURU! I appreciate so much! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change values in .conf file with a script

This is my first time posting here...so be gentle. Suppose I have a test.conf file that contains a value such as a IP. I would like to be able to use the Dialog Utility in Linux to allow me to enter the new IP in a Dialog form...and the results get saved into the test.conf file in the place... (4 Replies)
Discussion started by: calahanp
4 Replies

2. Shell Programming and Scripting

Perl script to change the date in some scenario

Hi , I have file FSN.log which contains number 100. i have other Perl script when i run it , it ll increment this FSN.log value. now my requirement is when the count in FSN.log becomes 999, it should make the value to 100 again and Perl script to change the date or it should make the date... (2 Replies)
Discussion started by: santhoshks
2 Replies

3. Shell Programming and Scripting

Change value in a file using perl or shell script

hi, I have a local.conf file which has the first line TOPDIR = "/home/mvdev/workspace/boxer". I want to replace the value to "/home/common/workspace/mirror". I tried the following perl command that is perl -p -i -e 's/Path/path1/g' myfile.txt then sed... (7 Replies)
Discussion started by: amvarma77
7 Replies

4. Shell Programming and Scripting

Passing PERL var values to SH Shell Script

Greetings all, If I have a SH script that calls a PERL script in the following way: perl $HOME/scripts/config.properties And in the config.properties PERL file, this Perl script only sets a number of environmental parameters in the following way: #!/usr/bin/perl $VAR1 = ( ... (3 Replies)
Discussion started by: gikyo12
3 Replies

5. Shell Programming and Scripting

(Perl or Unix) Script to get values from two files?

Hello, I am working on a script to get values from 2 files, and write to a new result file. File 1: 8-columned file separated by space: SerialNumber-Abc123 ID-1234:0 Name-xxx Class-yyy Object-zzz Source-aaa Value-bbb Comments-ccc SerialNumber-Abc124 ID-1234:1 Name-xxx Class-yyy... (1 Reply)
Discussion started by: ad23
1 Replies

6. Shell Programming and Scripting

Change values in Log4j.xml using ksh script

Hi, I am new to UNIX and shell scripting. I have to create a shell script(ksh) which parses log4j.xml file for a given webservice name and change the corresponding value from INFO to DEBUG or vice-versa. My log4j.xml looks like:- <!-- Appender WEBSERVICENAME--> <appender... (3 Replies)
Discussion started by: sanjeevcseng
3 Replies

7. Shell Programming and Scripting

How to change values in certain column only in every line (any script)

Let say in a file I have lines of data like this : 13;2073;461496;15075341;3;001f7d3a;2042063674; 13;2074;461446;15080241;6;001ed33a;2042020154; 13;2075;461401;15085270;6;001f593b;2042054459; 13;2076;461381;15087160;6;001f7483;2042061443; 13;2077;461419;15083419;6;001eca1a;2042017818; I... (3 Replies)
Discussion started by: luna_soleil
3 Replies

8. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

9. Shell Programming and Scripting

How to change a directory on windows via perl script

How to change a directory on windows via perl script. I wanna mount a share on windows via perl script. I have used:- system("dir"); gives the list of directories in the drive i am.This command works well. Now i want to move from c drive to z drive via perl script.I am using:- ... (2 Replies)
Discussion started by: TRUPTI
2 Replies

10. Shell Programming and Scripting

how to change "set" values in perl, windows...

i am using perl in win2000advanced server... --------------------------- perl -version: --------------------------- This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 638 provided by... (1 Reply)
Discussion started by: sekar sundaram
1 Replies
Login or Register to Ask a Question