Sponsored Content
Top Forums Shell Programming and Scripting Insterting column in csv from multiple files Post 302889586 by g9100 on Friday 21st of February 2014 05:24:55 PM
Old 02-21-2014
Hi,

Seems that I managed to get the result I wantend :-D
Mainly an outputfile.csv file that I can import to my website
I decided to manually edit the specfile from a 2line spec to a 1line spec So I can use this script on other products too.
Although it seems to work fine ( I opened the outputfile.csv file with libreoffice and seems to be OK) I hope that I could get a final comment from you regading this script as this is my first attemt with perl and have almost no clue of what I am doing :-)
So here it goes:

Code:
#!/usr/bin/perl -w

my $in_file_2   =  'specfile.txt';             ## file with quoted strings
my $in_file_1   =  'csvfile.csv';             ## csv file with text to join info from file_1 to
my $out_file    =  'outputfile.csv';
my $line_f1;
my $line_f2;
my $out_line;

# hash for values that have a replacement with value
my %replacement_values = (
 'PC'        => 'PC',
 'Core,'     => 'Core',
 '3,'        => '3',
 '7,200'     => '7200',
 'site'      => 'site',
 'Cache,\)'  => 'Cache',
 '0\)'        => '0',
 'Graphics)' => ' ',
 'GHz GHz\)'  => ' ',
 'GHz) GHz\)' => ' ',
 'Non-ECCz'  => ' ',
 'Turbo'     => ' ',
 '12,04'     => '12.04',
 '86W\)'      => ' ',
 'Warranty:' => ' ',
 'Intel'      => ',Intel',
 '00)'      => ',00',
 '(D'      => 'D'

);

open ( my $in_file_fh_1, '<', $in_file_1  ) or die "Can't Open $in_file_1 $!\n";
open ( my $in_file_fh_2, '<', $in_file_2  ) or die "Can't open $in_file_2 $!\n";
open ( my $out_file_fh,  '>', $out_file   ) or die "Can't open $out_file $!\n";

while ( ! eof( $in_file_fh_1 ) and ! eof( $in_file_fh_2 ) ) {
  $line_f2    =  <$in_file_fh_2>;
    chomp $line_f2;
    # perform hash replacements
  ( $out_line =  $line_f2 )  =~  s/(@{[join '|', map { quotemeta($_) } keys %replacement_values]})/$replacement_values{$1}/g;
  # Remove specific text from line
  $out_line  =~  s![0-9]MB,*| *w *\/ *| Internal Dell Business Audio Speaker,| {2,}!!g;
  # Remove begininng and ending double quote
  $out_line  =~  s!^\"!!;
  $out_line  =~  s!\"$!!;

  $line_f1   =  <$in_file_fh_1>;
    chomp $line_f1;
  # You could then split line($out_line) into array and write to out_file formatted as desired with with data from
  # csv file, html, etc.
  print $line_f1 . $out_line . "\n";
  print $out_file_fh $line_f1 . "\' \n";
  $work_line = $line_f1 . $out_line . "\n";


  # Split line into fields array and print each element for visual reference/debugging
  @fields  =  split( ( ',' ),$work_line );
  for $fields_index ( 0 .. $#fields ) {
      print $fields[$fields_index] . "\n";
  }
  print "\n";
  # Create output formatted lines with text and fields from array
  for $fields_index ( 0 .. $#fields ) {
    $out_line = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"> \n" .
                "<tbody>\n" .
                "<tr>     \n" .
                "<td class=\"specsTitle\">    Case    </td> \n" .
                "<td class=\"specsDescript stripeBottom\">    Standard Desktop Tower Chassis     </td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle highlightRow\">    CPU    </td> \n" .
                "<td class=\"specsDescript stripeBottom highlightRow\">$fields[3]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle\">    Operating System    </td> \n" .
                "<td class=\"specsDescript stripeBottom\">$fields[8]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle highlightRow\">    Hard disk    </td> \n" .
                "<td class=\"specsDescript stripeBottom highlightRow\">$fields[6]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle\">    Memory     </td>\n" .
                "<td class=\"specsDescript stripeBottom\">$fields[5]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle highlightRow\">    Optical Drive    </td> \n" .
                "<td class=\"specsDescript stripeBottom highlightRow\">$fields[7]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle\">    VGA    </td> \n" .
                "<td class=\"specsDescript stripeBottom\">    Mobile Intel® Graphics Media Accelerator $fields[4]</td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle highlightRow\">    Keyboard    </td> \n" .
                "<td class=\"specsDescript stripeBottom highlightRow\">    Dell Keyboard    </td> \n" .
                "</tr>     \n" .
                "<tr>     \n" .
                "<td class=\"specsTitle\">    Mouse    </td> \n" .
                "<td class=\"specsDescript stripeBottom\">    Dell USB mouse    </td> \n" .
                "</tr>     \n" .
		 "<tr>     \n" .
                "<td class=\"specsTitle\">    Warranty    </td> \n" .
                "<td class=\"specsDescript stripeBottom\">  $fields[9] </td> \n" .
                "</tr>     \n" .
                "</tbody>     \n" .
                "</table>    \n \'";
  }
  print $out_file_fh $out_line . "\n";     # Print formatted line to outfile

}

Kind Regards
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Update a column value in csv files

Hi all I am new to scripting and i have an application from which i will export into a csv file as follows Column1, Column2, Column3 Sno1, Folder\Test.txt, Fail Sno2, Folder\Test1.txt, Pass Sno3, Folder\Test2.txt, Fail Now i need to change the column2 in all the rows from "Folder\" ... (1 Reply)
Discussion started by: rajeshrp
1 Replies

2. Shell Programming and Scripting

Combine Multiple text or csv files column-wise

Hi All I am trying to combine columns from multiple text files into a single file using paste command but the record length being unequal in the different files the data is running over to the closest empty cell on the left. Please see below. What can i do to resolve this ? File 1 File... (15 Replies)
Discussion started by: venky_ibm
15 Replies

3. Shell Programming and Scripting

Sar -u generates multiple column headers in csv file

Hi All, The below sar -u command generates multiple column headers in csv file Expected output should print column headers only once in the csv file shell script: $cat sar_cpu_EBS.sh #!/bin/bash while ; do sar -u 15 1 | awk '/^/ {print $1,$2,$4,$6,$7}' | tr -s ' ' ',' >>... (6 Replies)
Discussion started by: a1_win
6 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. UNIX for Dummies Questions & Answers

Merge two csv files using column name

Hi all, I have two separate csv files(comma delimited) file 1 and file 2. File 1 contains PAN,NAME,Salary AAAAA5467D,Raj,50000 AAFAC5467D,Ram,60000 BDCFA5677D,Kumar,90000 File 2 contains PAN,NAME,Dept,Salary ASDFG6756T,Karthik,ABC,450000 QWERT8765Y,JAX,CDR,780000... (5 Replies)
Discussion started by: Nivas
5 Replies

6. Shell Programming and Scripting

Row bind multiple csv files having different column headers

All, I guess by this time someone asked this kind of question, but sorry I am unable to find after a deep search. Here is my request I have many files out of which 2 sample files provided below. File-1 (with A,B as column headers) A,B 1,2 File-2 (with C, D as column headers) C,D 4,5 I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

7. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

8. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

9. UNIX for Beginners Questions & Answers

How do I extract specific column in multiple csv files?

file1: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,47 GETregistry,100,21592,0.00%,13 GEThomeLayout,100,30466,0.00%,17 file2: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,33 GETregistry,100,21592,0.00%,22... (6 Replies)
Discussion started by: Raghuram717
6 Replies

10. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies
All times are GMT -4. The time now is 03:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy