- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Perl script from tab deliminated to , deliminated
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 05:05 AM
тАО09-20-2004 05:05 AM
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;
...
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 05:25 AM
тАО09-20-2004 05:25 AM
Re: Perl script from tab deliminated to , deliminated
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 06:00 AM
тАО09-20-2004 06:00 AM
Re: Perl script from tab deliminated to , deliminated
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 06:13 AM
тАО09-20-2004 06:13 AM
Re: Perl script from tab deliminated to , deliminated
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 07:01 AM
тАО09-20-2004 07:01 AM
Re: Perl script from tab deliminated to , deliminated
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 07:02 AM
тАО09-20-2004 07:02 AM
Re: Perl script from tab deliminated to , deliminated
for LINE in [filename]; do
echo $LINE|tr -s [:space:] |tr -s " " "," > [newfilename]
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 08:44 AM
тАО09-20-2004 08:44 AM
Re: Perl script from tab deliminated to , deliminated
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 04:56 PM
тАО09-20-2004 04:56 PM
Re: Perl script from tab deliminated to , deliminated
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 05:13 PM
тАО09-20-2004 05:13 PM
Solution/\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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2004 06:15 PM
тАО09-20-2004 06:15 PM
Re: Perl script from tab deliminated to , deliminated
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2004 03:28 AM
тАО09-21-2004 03:28 AM
Re: Perl script from tab deliminated to , deliminated
Now on to another little glich regarding the same script. But I will post new on this subject.