Perl substitution

Hi,

I'm new to Perl, and I want to change a few columns in a file in order to insert them into a database.

The input file looks like this:

00001,"01/1234567" ,"Tst2"
00002,"01/4545646" ,"Tst123456"
00003,"01/8979898" ,""

The output should look like this:

01-1234567,00001
01-4545646,00002
01-8979898,00003

I know how to do it from the command line, but how do I do it from within a script?

This is what I use when running it from the command line:

perl -i -ne 'if(/(\d+),\"(\d+)\/(\d+)\"/) { printf("%s-%s,%s\n", $2,$3,$1); }' file.txt

I tried something like this, but it doesn't work:

@res =~ s/(\d+),\"(\d+)\/(\d+)\"/\2-\3,\1/';

(@res contains a line from the file)

------------------------------------
EDIT: Never mind, I found the solution myself. I simply needed to store it in a variable instead of an array:

$l =~ s/(\d+),\"(\d+)\/(\d+)\"/$2-$3,$1/

If you are loading into Oracle using sqlldr, you can specify what you want through a load control file without the need to change the input file. Other loading utilities may have similar capabilities.

1 Like
perl -pe 's/(.+?),"(.+?)\/(.+?)\"(.*)/$2-$3,$1/g;'  file.txt