CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
easy way to convert text file data in column to one row

 



Bill_Koz
New User

Jun 11, 2018, 6:53 AM

Post #1 of 6 (4530 views)
easy way to convert text file data in column to one row Can't Post

Hello, new to perl and the forum.
I have a file with text in a column

c:\Entity.txt

051005
620005
007029

I would like to transform to a new text file with data in one row.
would look like.
c:\Entity_clear.txt

[051005],[620005],[007029]

original text file would have different data each month

Any lead would be appreciated.
Thank you


BillKSmith
Veteran

Jun 11, 2018, 8:54 AM

Post #2 of 6 (4525 views)
Re: [Bill_Koz] easy way to convert text file data in column to one row [In reply to] Can't Post

Hello, Bill. Welcome to perlguru.

Please be aware that we want to help, not do your work for you. We want to see what you have tried. This helps us know what kind of help you need.

Are you new to programming or just to perl? Could you write this program in any other language? Did you try "hello world" in Perl? Did you succeed? Have you written any other programs in Perl? Is this a homework assignment? If so, what are you studying?

Perhaps this will help.
Slurp the file into any array of lines.
Remove the newline characters with chomp.
Print the array to the screen as a string.
After that much works, modify the program
to print to a file.
Good Luck,
Bill


Bill_Koz
New User

Jun 11, 2018, 9:59 AM

Post #3 of 6 (4522 views)
Re: [BillKSmith] easy way to convert text file data in column to one row [In reply to] Can't Post

Thanks for the response,
I'm new to programming, this is for work, I inherited the perl scripts, I'm keeping the wheels on and learning from examples. I can chomp the file and print what I want to a new file, but the number of records will change. I'll pursue that route, if all else fails I will insist the source system give me the records in one row.


Laurent_R
Veteran / Moderator

Jun 11, 2018, 11:08 AM

Post #4 of 6 (4517 views)
Re: [Bill_Koz] easy way to convert text file data in column to one row [In reply to] Can't Post


In Reply To
I can chomp the file and print what I want to a new file, but the number of records will change.


That really shouldn't matter. You'll need to process the file lines in some form of loop (explicit or implicit), and the loop should simply run until there is no line left to process.

This is a one-liner doing almost what you want (but printing to the screen):


Code
perl -ne 'chomp; print "[$_],";' filename.txt


This will work on Linux or Mac , some changes would be needed under Windows.

Note that you can easily redirect the screen output to a file.


BillKSmith
Veteran

Jun 11, 2018, 11:23 AM

Post #5 of 6 (4515 views)
Re: [Bill_Koz] easy way to convert text file data in column to one row [In reply to] Can't Post

That would actually make it slightly harder!


Code
C:\Users\Bill\forums\guru>type Bill_Koz.txt 
051005
620005
007029

C:\Users\Bill\forums\guru>type Bill_Koz.pl
use strict;
use warnings;
chomp( my @lines = <> );
printf "[%s],[%s],[%s]\n", @lines;

C:\Users\Bill\forums\guru>perl Bill_Koz.pl Bill_Koz.txt >one_line.txt

C:\Users\Bill\forums\guru>type one_line.txt
[051005],[620005],[007029]


This should get you started. Its good enough for a one-time task, but you stated that this will be run monthly. A more professional version would open and close the files in perl rather than depending on the shell. It should also test for all likely errors, and take appropriate action. It is far easier to deal with these issues now than it to deal with your boss after someone else's mistake causes YOUR program to destroy a valuable file.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jun 11, 2018, 12:02 PM

Post #6 of 6 (4512 views)
Re: [Bill_Koz] easy way to convert text file data in column to one row [In reply to] Can't Post

The use of printf as Bill has shown is fine if there is a known and limited amount of number of lines and the line count never changes.

Here's an example that handles any number of lines but without the error checking/handling that Bill mentioned.


Code
#!/usr/bin/perl 

use warnings;
use strict;

chomp(my @data = <DATA>);
my $data = join ',', map { "[$_]" } @data;
print $data, $/;


__DATA__
051005
620005
007029


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives