Operating System - HP-UX
1820238 Members
2499 Online
109620 Solutions
New Discussion юеВ

Perl script from tab deliminated to , deliminated

 
SOLVED
Go to solution
Ratzie
Super Advisor

Perl script from tab deliminated to , deliminated

Right now I am doing alot of prework for a script.
Save data in word pad, then save a comma deliminated file, dos2ux, then run a perl script on that data.
I would like to eliminate some steps...

The file comes across as...
5555998 00 0 09 29 TELN NOT

There are lots of spaces inbetween columns, and at front of record.

I want it to look like
5555998,00 0 09 29,TELN NOT

I want to add this to the befinning of my script that starts like...

while (<>) {
chomp; # Will remove the leading , or new line
my @a = split /,/, $_, -1;
my $f = /TELN NOT BILL/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0],$CAPbld, $CAProom, $a[1], $a[2], "\n";
}
close $fc;
close $ft;
close $fo;
...
10 REPLIES 10
H.Merijn Brand (procura
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

chomp () will only remove the (optional) *trailing* newline. In fact it is the same as $_ =~ s{$/\Z}{};

any white space in front of that newline is preserved., so if you want to strip leading and trailing whitespace (including newlines),

while (<>) {
s/\s+$//;
s/^\s+//;

then just replace the /,/ with the tab

my @a = split m/\t/, $_, -1;

or, if you also want to strip leading and trailing spaces of every field,

my @a = split m/ *\t */, $_, -1;
note that you cannot use \s before the *, because \s includes \t

the print statement you use, will always add a ',' before the newline. If you don't want that, write

print $f join "," => $acode.$a[0],$CAPbld, $CAProom, $a[1], "$a[2]\n";

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: Perl script from tab deliminated to , deliminated

The problem I am having is that I have some columns that have white space inbetween, and it would split on that...
00 0 09 29 --> I need this so it would be 5555998,00 0 09 29,TELN NOT
TELN NOT --> I need this to be one column as well.
So Basically I would like it to parse the file, and add commas between columns and then save it to a file.
H.Merijn Brand (procura
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

Hard to do if the delimiter is not fixed. If it is a tab, and the other whitespace is just spaces, my solution will happily do what you asked it to do.

If there's no fixed delimiters, either tabs o commas, but the file has fixed-width columns, aligned with spaces, use unpack

my @a = unpack "A8 A13 A3 A24 A9 A45 A*", $_;

asuming columns - left to right, are 8, 13, 3, 24, 9, 45, and the rest wide.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Mark Greene_1
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

Try this script:

for LINE in [filename]; do
tr -s [:space:] |tr -s " " ","
done

It will first reduce all occuances of spaces and tabs to a single space, then substitute the space for a comma.

mark
the future will be a lot like now, only later
Mark Greene_1
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

Ok... let's try that again


for LINE in [filename]; do
echo $LINE|tr -s [:space:] |tr -s " " "," > [newfilename]
done
the future will be a lot like now, only later
Rodney Hills
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

If the columns that might have white space are at the end, then try-

perl -en '@a=split(" +",$_,5); print join(",",@a)'

The ,5 in split will only split up to 5 fields. Then the remaining text field is considered as just one field.

HTH

-- Rod Hills
There be dragons...
Ratzie
Super Advisor

Re: Perl script from tab deliminated to , deliminated

I am trying to get this to work but I am getting an error...

user@server$ more test.pl
#!/opt/perl/bin/perl

use strict;
use warnings;


while (<>) {
chomp; # Will remove the leading , or new line
s,^\s+,,; #Remove leading spaces
my @cols=split(/\s+{2,}/,$_); #Split on two (or more) spaces
print join (',',@cols)."\n";
}


This is the error...

Nested quantifiers in regex; marked by <-- HERE in m/\s+{ <-- HERE 2,}/ at ./test.pl line 10.

Hein van den Heuvel
Honored Contributor
Solution

Re: Perl script from tab deliminated to , deliminated

Well, the error message comes really close to identifying the problem:

/\s+{2,}/

Here, the the "+" means: "one or more spaces"
The "{2,}" means: Two or more of the prior. But 'prior' is this 'one or more' thing. Too tricky!

Fix that by using either: \s{2,}
or: \s\s+

For example:

#!/opt/perl/bin/perl

use strict;
use warnings;


while (<>) {
chomp; # Will remove the leading , or new line
s,^\s+,,; #Remove leading spaces
my @cols=split(/\s{2,}/,$_); #Split on two (or more) spaces
print join (',',@cols)."\n";
}


Cheers,
Hein.

H.Merijn Brand (procura
Honored Contributor

Re: Perl script from tab deliminated to , deliminated

And it has still very misleading comments!

re-read my chomp comments
\s is not a space! it is white space: tabs, new-lines, spaces, \x{85}, \x{2028}, \x{2029}, but not VT

#!/opt/perl/bin/perl

use strict;
use warnings;

while (<>) {
chomp; # Will remove the optional trailing new line
s,^\s+,,; #Remove leading spaces
my @cols=split m/\s{2,}/, $_, -1; # Split on two (or more) white space characters
print join (',', @cols), "\n";
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: Perl script from tab deliminated to , deliminated

Excellent I have it working thank you to all who have helped out.
Now on to another little glich regarding the same script. But I will post new on this subject.