Perl: parsing variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: parsing variables
# 1  
Old 07-16-2008
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  
Old 07-16-2008
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 Smilie
# 3  
Old 07-16-2008
That works excellent, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Parsing a file and setting to variables.

greetings all, I have a task right now that is somewhat stumping me, and I am not sure what the best approach is to take it. I have a text file that will contain something similar to the following: first1, other1 first2, other2 first3, other3 first4, other4 I have to generate an... (14 Replies)
Discussion started by: jeffs42885
14 Replies

3. Shell Programming and Scripting

Parsing fields into variables

A record contains 50 fields separated by "~". I need to assign each of these fields to different variables. Following is the shell script approach I tried. RECORD="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5~...........~FIELD50" VAR1=$(echo ${RECORD} | cut -d"~" -f 1) VAR2=$(echo ${RECORD} | cut... (5 Replies)
Discussion started by: krishmaths
5 Replies

4. UNIX for Dummies Questions & Answers

Parsing alphanumeric variables

Hi All, I have files with a column which has values and ranges, for example colA colB ERD1 3456 ERD2 ERD3 4456 I want to have the following output colA colB colC ERD1 3456 3456 ERD2 526887 526890 ERD3 4456 4456 Being a newbie to... (2 Replies)
Discussion started by: alpesh
2 Replies

5. Shell Programming and Scripting

parsing argument in perl

in bash: LIST=`cat $1` for i in $LIST do ... done how will i do this in perl ? $1 is my first arguement. I'm a newbie in perl and will appreciate much your help guys ... (4 Replies)
Discussion started by: linuxgeek
4 Replies

6. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies

7. Shell Programming and Scripting

Help parsing logs maybe with menu and variables?

I would like to parse through some logs looking for things like exception or failed (grep -i failed). Ideal would be if it were in a menu format so someone without unix ability could just choose option 1 2 or 3 etc. If I could pass the hostname to a variable also that would be awesome, so someone... (5 Replies)
Discussion started by: taekwondo
5 Replies

8. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

9. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies

10. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies
Login or Register to Ask a Question