![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| xml parsing error in perl | bishweshwar | UNIX for Advanced & Expert Users | 1 | 05-30-2007 11:59 PM |
| HTML parsing by PERL | avik1983 | Shell Programming and Scripting | 3 | 02-23-2007 09:25 AM |
| Parsing and getting values of variables | Rekha | Shell Programming and Scripting | 3 | 08-01-2006 11:39 AM |
| PERL - Parsing Crystal Reports | srinivay | Shell Programming and Scripting | 0 | 05-12-2005 08:55 AM |
| Conversion of bash parsing script to perl? | cstovall | Shell Programming and Scripting | 2 | 10-13-2004 11:33 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Perl: parsing variables
I have the following script:
Code:
#!/usr/bin/perl -w
@files = <*.csv>;
foreach $file (@files) {
open(FH, $file);
my @dt = split(/_|.csv/, $file);
while (<FH>) {
chomp;
print $dt[1] . $dt[2] . ",$_\n";
}
close(FH);
}
A filename is composed of a name, a date, a time and a suffix ".csv". So a filename could be foo_20080909_120345.csv Upon running this, the @dt array holds: - $dt[0] = "foo" - $dt[1] = 20080909 - $dt[2] = 120345 So line 9 (print $dt[1] . $dt[2] . ",$_\n") yields lines of the following: "20080909 120345,[...rest of the record...]" where it should yield: "2008-09-09 12:03:45,[...rest of the record...]" How do I enter dashes ("-") and colons (":") at the right places without using contrived code such as: substr($dt[1], 0, 4) . "-" . substr($dt[1], 4, 2) . "-" . substr($dt[1], 6, 2) and that is just for the date. Thanks in advance |
|
||||
|
Here are my files:
Code:
bar_20081009_113023.csv foo_20080909_120345.csv munge_20061231_010020.csv Code:
#!/usr/bin/perl -w
@files = <*.csv>;
foreach $file (@files) {
open(FH, $file);
my @dt = ($file =~ /^(\w+)_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\.csv$/);
while (<FH>) {
chomp;
print "$dt[1]-$dt[2]-$dt[3] $dt[4]:$dt[5]:$dt[6],$_\n";
}
close(FH);
}
Code:
# ./test.pl 2008-10-09 11:30:23,bar1 2008-10-09 11:30:23,bar2 2008-09-09 12:03:45,foo1 2008-09-09 12:03:45,foo2 2006-12-31 01:00:20,munge1 2006-12-31 01:00:20,munge2 ![]() |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|