Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl put lines in a file in to a single file

Tags:

linux

perl

I have a file called mail.txt in which lines are printed like below ,i want to put all those lines in to one single line like

Thanks


This is the input

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>
q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>

This is the output

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<[email protected]>,<[email protected]>

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred), <[email protected]>,<[email protected]>
like image 919
Yagyavalk Bhatt Avatar asked Dec 08 '25 13:12

Yagyavalk Bhatt


1 Answers

So why don't you just do it?

open(my $fh, "<", $input_filename);

my @lines = map { chomp; $_} <$fh>; #1

close $fh;

open(my $out, ">", $output_filename);

print $out join "", @lines; # or maybe a different separator, like ","

close $out;

#that's it

Note: if you want to get rid of the extra spaces at the beginnings and ends of input lines, you can replace line #1 by

my @lines = map { s/\s+$//; s/^\s+//; $_} <$fh>;
like image 107
jpalecek Avatar answered Dec 10 '25 03:12

jpalecek