Grabbing fields with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grabbing fields with perl
# 1  
Old 02-24-2013
Grabbing fields with perl

I have several .csv files containing data like this:

field_1;field_2;date;comment;amount;


I want to extract the 3 last fields and load them in a database.


Code:
my input_file = "/dir/file.csv";
my output_file = "/dir/file.sql";

open my $csv_file, '<', $input_file
        or die "Can't open file '$input_file': $OS_ERROR";
my @lines = <$csv_file>;
close $csv_file
        or die "Can't close '$input_file' after reading: $OS_ERROR";
    
open my $sql_file, '>', $output_file
        or die "Can't open file '$sql_file' : $OS_ERROR";
    
for my $line (@lines) {
#Now I would like to split every line in an array of field, each field being separated by ';'
my @fields = ???;
# please I need some help here...

print $sql_file $fields[3];
print $sql_file ' ';
print $sql_file $fields[4];
print $sql_file ' ';
print $sql_file $fields[5];
print $sql_file "\n";
}
close $sql_file or die "Can't close '$output_file' after reading: $OS_ERROR";



It is simply done by:

Code:

awk -F';' '{print $3 $4 $5}' /dir/file.csv


with awk but what about perl?

Thank you for your help and keep up the great work!
freddie50
# 2  
Old 02-24-2013
Code:
my @fields = split /;/, $line;

You should also call "chomp" after reading in the lines, so that newline characters won't end up in your fields:
Code:
chomp(my @lines = <$csv_file>);

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing fields without using external commands

data: bblah1_blah2_blah3_blah4_blah5 bblahA_blahB_blahC_blahD_blahE im using the following code to grab the field i want: cat data | while IFS='_' read v1 v2 v3 v4 v5; do printf '%s\n' "${v4}"; done im catting data here. in the real world, the exact content of data will be in a variable.... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

Using to perl to output specific fields to one file

Trying to use perl to output specific fields from all text files in a directory to one new file. Each text file on a new line. The below seems to work for one text file but not more. Thank you :). perl -ne 's/^#//; @n = (6, 7, 8, 16); print if $. ~~ @n' *.txt > out.txt format of all text... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Perl search multiple fields

Hi all, I have a flatfile 300 lines tiger,tampa10-pc,yellow,none,2013-02-25 08:56:51.000,2013-02-25 21:41:11.380,12hrs : 44min cat,tampa10-pc,white,none,2013-02-28 08:56:58.000,2013-03-04 23:18:23.003,110hrs : 21min dog,tampa10-pc,yellow,none,2013-03-05 09:50:17.000,2013-03-07... (5 Replies)
Discussion started by: sabercats
5 Replies

4. Shell Programming and Scripting

Pattern Matching and extracting the required fields in Perl

Hi All, I am writing the following Perl Scrip and need your help in Pattern matching : I have the following Shell Script that would read line by line from the file (file_svn) and would inturn calls the Perl Script: #!/bin/bash perl_path="/home/dev/filter"... (2 Replies)
Discussion started by: filter
2 Replies

5. Shell Programming and Scripting

Grabbing web forms with Perl

Hello Everyone, I've googled everywhere for this with no luck and my brain hurts. I'm trying to write a program to take my webpages and search for forms. I've used LWP::Simple to store a website in $content I need to cut the form out of $content. I don't know how to do this seeing as how... (2 Replies)
Discussion started by: wibbs
2 Replies

6. Shell Programming and Scripting

Perl: Parse Hex file into fields

Hi, I want to split/parse certain bits of the hex data into another field. Example: Input data is Word1: 4f72abfd Output: Parse bits (5 to 0) into field word1data1=0x00cd=205 decimal Parse bits (7 to 6) into field word1data2=0x000c=12 decimal etc. Word2: efff3d02 Parse bits (13 to... (1 Reply)
Discussion started by: morrbie
1 Replies

7. Shell Programming and Scripting

how to match fields from different files in PERL

Howdy! I have multiple files with tab-separated data: File1_filtered.txt gnl|Amel_4.0|Group3.29 1 G R 42 42 60 15 ,.AAA.aa,aa.A.. hh00/f//hD/h/hh gnl|Amel_4.0|Group3.29 2 C Y 36 36 60 5 T.,T, LggJh gnl|Amel_4.0|Group3.29 3 A R 27 27 60 9 Gg,,.gg., B6hcc22_c File2_filtered.txt ... (3 Replies)
Discussion started by: sramirez
3 Replies

8. Shell Programming and Scripting

Grabbing Certain Fields

ok, so a script i wrote spits out an output like the below: 2,JABABA,BV=114,CV=1,DF=-113,PCT=99.1228% as you can see, each field is separated by a comma. now, how can I get rid of the first field and ONLY show the rest of the fields. meaning, i want to get rid of the "2,", and... (3 Replies)
Discussion started by: SkySmart
3 Replies

9. Shell Programming and Scripting

grabbing specific column perl

Alright, I'm new to Perl so be gentle. Given the following script: ---- open(file, "<file.txt"); @lines = <file>; close(file); $var = print $lines; ---- So I'm printing line 18 of the file "file.txt". I now want the 5th column, minus the forward slash. The line looks like this: ... (2 Replies)
Discussion started by: wxornot
2 Replies

10. Shell Programming and Scripting

getting the date fields in Perl

I get the perl date in the following format: Mon Jan 31 06:01:37 2005 How can I extract each field in a single line using a regular expression?? I dont want to use "split". Any other way out?? JP :) (2 Replies)
Discussion started by: jyotipg
2 Replies
Login or Register to Ask a Question