Saturday, July 23, 2011

joining the lines in file using comma perl

Content of list.txt:

Hi gopal,how are you
Hi james,how are you
Hi,how are you
Hi Thomas,how are you
Hi Manjula,how are you
Hi karthik,how are you

Content of the Perl file:

#!/usr/bin/perl
use File::Copy;
open(FH,";
close(FH);
#For creating the temp file and adding the line no
open(FH1,">list.txt.bak");
$dd=join(',',@ddd); # for joining the lines in the file
$dd =~ s/\n//sg; # for removing the newline character
print FH1 $dd;
close(FH1);
move("list.txt.bak","list.txt") or die "cannot move";
# for moving the temp file to the original files
unlink("list.txt.bak") or die "cannot delete the file";
# for removing the temp file

Content of the list.txt after execution of perl file:

Hi gopal,how are you,Hi james,how are you,Hi,how are you,Hi Thomas,how are you,Hi Manjula,how are you,Hi karthik,how are you

Analysis:
Here i am trying to join all the lines of the file list.txt and separate the lines by comma.To join all the lines i have used join command and replaced "\n" with space using substution in perl.

No comments:

Post a Comment