The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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 10: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 10:39 AM
PERL - Parsing Crystal Reports srinivay Shell Programming and Scripting 0 05-12-2005 07:55 AM
Conversion of bash parsing script to perl? cstovall Shell Programming and Scripting 2 10-13-2004 10:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
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);
}
This script reads in all csv files in the current directory and prints the date, time and complete contents to screen.

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
  #2 (permalink)  
Old 07-16-2008
Diabolist Diabolist is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 44
Here are my files:

Code:
bar_20081009_113023.csv
foo_20080909_120345.csv
munge_20061231_010020.csv
Here's the script:
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);
}
Here's the output:
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
I'm sure it can be cleaned up a little. I'm not a very good with perl
  #3 (permalink)  
Old 07-16-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
That works excellent, thanks.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0