First, don't start by attacking the loop. That's just a simplification so that you don't have each and every file manually. Instead, develop the necessary steps using 1 file as example, in a way that can be applied to all other files.
The first thing you want to do with each file is remove the first 2 lines, and the last one. This can easily be done using sed:
Next, you want to remove all empty lines. This can be included with the previous sed statement:
Once that's done you want to surround the values with and SQL INSERT statement. For that, we need a second sed statement:
This can be read as "On the first line, replace the beginning of the line with this string.On all but the last line, replace the end of the line with a comma.On the last line, append the string to the end of the line."
Now that a single file can be processed as needed, we can start working on the loop. A simple for loop is all that is needed:
But wait! The table used should be the one the file is named after, so we'll have to use that somewhere too. For that we need basename, and we have to alter the second sed statement a bit (marked in green)
That's all there is to it. There's no magic, arcane knowledge, or infinite UNIX wisdom needed as long as you can break down the steps as far as needed, and are not afraid of reading the man pages for the commands you think you'll need.
when i try the same its giving syntax error.do we need to have add some thing ?
HTML Code:
./column.sh: line 14: syntax error near unexpected token `('
./column.sh: line 14: ` sed -e '1s/^/INSERT INTO '${filename}' VALUES(/; $!s/$/,/; $s/$/);/''
That's an error message from the shell. Can you post the part of the complete script where you're using the loop, including the surrounding lines (copy-paste please)?
yes its working party but ouput is coming like this Even i don't want the Last line "3 records(s) selected" for each file ouput.
sed: can't read sed: No such file or directory
do we need to edit some thing else? i could not seen any INSERT statement or braces "( ) " printed.
Yes, I've missed another typo (shame on me). There should be a pipe before the backslash, again on the first sed line. I've corrected and marked it in my previous post.
I have a script that sorts and processes unsorted files to newly created directories.
Its working great, but I am trying to understand the leanest method to get the script to create an additional file within each newly created directory that:
Contains a list of all files in the directory... (4 Replies)
Hi ,
i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help.
I dont want to use cat command to read. Can we do it with out using cat command.
while read line
do... (9 Replies)
Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form
File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Data
I was trying to write shell script which will be return the output in the below format
First i was trying to do these using sed.
sed -n '/.ksh/p' mainksh.ksh
sed -e 's/*\(.*\)/\1/g' mainksh.ksh
$RUN_DIR, $SUB_DIR and the variables which will be defined in the profile file.
when i am... (0 Replies)
Have to read one file say sourcefile containing several words and having another folder containing several files.
Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Hey,
I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file.
So file1:
1 1 0 1
2 1 1 2 ... (7 Replies)
Hi,
I am reading a file line by line using read line function of while loop. Each line contains 4 fields. I want to take these 4 values in 4 variables in each iteration so that i can use them in my script. The issue here is that my awk command is returning awkward results -
Here is a sample line... (8 Replies)
Hi Gurus,
I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file.
eg. if file a.txt exists then send a mail to a@a.com
simillary for b.txt,c.txt etc.
Thanks for your help,
Nimu (6 Replies)
Hi,
Thanks in Advance..
i have the following requirement, some one please help me..
An unix shell script has to pick up the file name from a folder as soon as that file comes into that folder.
Regards,
Alo (6 Replies)