Click to See Complete Forum and Search --> : Perl inplace editing
Linux_cat
08-15-2007, 08:53 AM
Hello, im now on a quest to learn perl for some data testing i have to do..
I am writing a perl script to edit a data file delimeted by a tab and take a particular field specified by a text file and edit the value using substr, i need to do this for multiple files but ive hit my first hurdle, i can only get my script to edit data for 1 row!!
#!/usr/bin/perl
#set input of the first text file
open(TXT, "<$ARGV[0]");
my @text = <TXT>;
close(TXT);
#set input of input field file
open(I_FIELD, "<$ARGV[1]");
my $input = <I_FIELD>;
my @field = split(',', $input);
close(I_FIELD);
#print output
foreach $line(@text)
{
my @data = split(' ', $line);
substr(@data[@field[1]], 4,8) = "123@." ;
print "@data[@field[0]]|@data[@field[1]] \n";
}
i get the following output:
00224220F19970731AB1| H123@.HOUSE
substr outside of string at ./arg.pl line 18.
when i run it without the substr it prints all lines in the file can someone explain to me what im doing wrong, im a total newbie to perl and i cant find anything on the next that can help me with this!!
ghostdog74
08-16-2007, 11:00 PM
I had always thought to reference array elements you use $ instead of @?
eg $data[0] ?? well.correct me if i am wrong though.
flukshun
08-23-2007, 08:40 AM
Hello, im now on a quest to learn perl for some data testing i have to do..
I am writing a perl script to edit a data file delimeted by a tab and take a particular field specified by a text file and edit the value using substr, i need to do this for multiple files but ive hit my first hurdle, i can only get my script to edit data for 1 row!!
#!/usr/bin/perl
#set input of the first text file
open(TXT, "<$ARGV[0]");
my @text = <TXT>;
close(TXT);
#set input of input field file
open(I_FIELD, "<$ARGV[1]");
my $input = <I_FIELD>;
my @field = split(',', $input);
close(I_FIELD);
#print output
foreach $line(@text)
{
my @data = split(' ', $line);
substr(@data[@field[1]], 4,8) = "123@." ;
print "@data[@field[0]]|@data[@field[1]] \n";
}
i get the following output:
00224220F19970731AB1| H123@.HOUSE
substr outside of string at ./arg.pl line 18.
when i run it without the substr it prints all lines in the file can someone explain to me what im doing wrong, im a total newbie to perl and i cant find anything on the next that can help me with this!!
its been a while since ive done any scripting, but let me get this straight, you basically want to edit a particular column value from an input stream, then spit out the entire editted row for every line in the input stream?
substr is generally to extract a particular string from a given string. i.e. substr($line, 4, 8) would move to the 4th character of $line, and return the next 8 characters as a string. this does not give you access to the original string, i.e. you cant assign a value to this, which is what you seem to be doing here.
your intentions arent clear enough for me to really give you any direct advice, but if you goal is to take $data[@field[1]], 4,8) = "123@." ; (i believe ghostdog is right, you access a scalar value from an array as $blah[0] rather than blah @blah[0], but in this case i think perl lets you get away with it), and substitute characters 4-12 from that value with "123@.", you could do something like:
my $newValue = substr($data[@field[1]], 0,4) . "123@." . substr($data[@field[1]], 13);
i.e. retain the first 4 characters, append "123@.", then append all remain characters of the value starting at the 13th character. in essence you've substituted characters 4-12 with "123@."
but like i said im not sure of oyur intentions so this may be way off fromw hat you actually want to do.
hth
Bryon Speede
08-24-2007, 02:03 PM
I am finding it difficult to follow your script, bit first try changing
my @data = split(' ', $line);
to
my @data = split(/\t/, $line);
That will use PCRE to match the tab. It looks from your output that you have some spaces padding your second field.
Linux_cat
08-27-2007, 11:25 AM
Thanks guys, ive finally figured it and written the script, ill post the script and how it works when i get back to work in a bid that it may assist someone in the future....
Thanks again for all the help!