Sponsored Content
Top Forums Shell Programming and Scripting Need help with extracting data to MySQL format Post 302575065 by Skrynesaver on Sunday 20th of November 2011 07:10:34 AM
Old 11-20-2011
Quote:
Originally Posted by Johanni
I got these errors
Code:
$ data2.sh
/usr/bin/data2.sh: line 2: use: command not found
....

use is a Perl command , try renaming the file data2.pl and then runchmod +x ./data2.pl to make the script runnable.
Quote:
Originally Posted by Johanni
I input the file like this:
Code:
open (my $data, "<", $temp1.dat[0]);

is it correct?
Probably not, the original script is intended to be runnable as follows...
Code:
./datas2.pl PATH_TO_ORIGINAL_DATA_DUMP_FROM_LYNX

$ARGV[0] is the first argument provided to the script on the command line.

And here's the code after fixing up the bugs.
Code:
#!/usr/bin/perl

#These two pragmas catch typos for you.
use strict; 
use warnings;

open (my $data, "<", $ARGV[0]); #Supply the spec file as the argument
my $after_spec=0; #flag which is set if the line is a spec line.
my @values; # an array to hold all the lines that match
while(<$data>){ # loop through the file one line at a time
   chomp; #remove trailing newlines.
   if ($after_spec){ # if we set the flag on the previous line.
      push @values, $_; # add this string to the values array
      $after_spec--; # unset the flag.
   }
   elsif (/^Spec$/){ # if the line matches the pattern 
      $after_spec++; # set the flag
   }
}
for (@values){ #go through the values array cleaning up the data.
   s/^\s*(.+)\s*$/$1/;#remove leading and trailing spaces
   s/'/\\'/g; # escape single quotes
   if (! /^\d+(\.\d+)?$/){ # if this is not a number
       $_="'".$_."'"; # surround the value with quotes
   }
}
my $values_string=join(', ',@values); # create a value string 
print "INSERT INTO `specifications` VALUES ($values_string);\n" # print the result.

Output with snippet of your data above
Code:
~/tmp$ ./tmp.pl tmp.dat 
INSERT INTO `specifications` VALUES (524543, 'No', 'Yes', 'Available using application', 'Android 2.3 (Gingerbread)', 'No', 'Yes', '0.34 W/kg', '710 h', '18.2 h', '8.4 h', '610 h', 'Mono', 'Yes', 'Yes', 'Yes', 'LED-flash');


Last edited by Skrynesaver; 11-20-2011 at 08:38 AM..
This User Gave Thanks to Skrynesaver For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies

2. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

3. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

4. Shell Programming and Scripting

Help needed to format mysql output

Hi all, Does anyone know how to format the output from mysql from within a shell script? i.e. RESULT=`mysql command` echo ${RESULT} the ${RESULT} only displays the output on one line instead of how mysql would display it as columns etc (3 Replies)
Discussion started by: muay_tb
3 Replies

5. Shell Programming and Scripting

Using Awk for extracting data in specific format

please help me writing a awk script 001_r.pdb 0.0265185 001_r.pdb 0.0437049 001_r.pdb 0.0240642 001_r.pdb 0.0310264 001_r.pdb 0.0200482 001_r.pdb 0.0146746 001_r.pdb 0.0351344 001_r.pdb 0.0347856 001_r.pdb 0.036119 001_r.pdb 1.49 002_r.pdb 0.0281011 002_r.pdb 0.0319908 002_r.pdb... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. UNIX for Dummies Questions & Answers

help in extracting logs in readable format

hello everyone. newbie here in unix. I am trying to extract the logs of a certain job and would like to output it in a readable format, see below the CAT part: cat /var/opt/ctma/ctm/sysout/idwesct_sh30_eng_r6_cdcs_sh.LOG_05l0du_000* | egrep -i 'orderid:|file_name=' | sed... (1 Reply)
Discussion started by: eanne_may
1 Replies

8. Shell Programming and Scripting

Need help getting my output from MYSQL query into right format

Good afternoon! I have been lurking in this forum for awhile now. I have just recently started posting. I think this is a really good site. With that being said, I don't like to just run and get an answer before I try my best first. I have poured some blood, sweat and tears into... (4 Replies)
Discussion started by: brianjb
4 Replies

9. Shell Programming and Scripting

Extracting content from a file in specific format

Hi All, I have the file in this format **** Results Data **** Time or Step 1 2 20 0.000000000e+00 0s 0s 0s 1.024000000e+00 Us 0s 0s 1.100000000e+00 1s 0s 0s 1.100000001e+00 1s 0s 1s 2.024000000e+00 Us Us 1s 2.024000001e+00 ... (7 Replies)
Discussion started by: diehard
7 Replies

10. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies
All times are GMT -4. The time now is 06:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy