Reading files under a folder and formatting content of each file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading files under a folder and formatting content of each file
# 8  
Old 02-11-2011
OK, let's tackle this step by step.

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:
Code:
sed -e '1,2d; $d' file

Next, you want to remove all empty lines. This can be included with the previous sed statement:
Code:
sed -e '1,2d; $d; /^[:space:]*$/d' file

Once that's done you want to surround the values with and SQL INSERT statement. For that, we need a second sed statement:
Code:
sed -e '1,2d; $d; /^[:space:]*$/d' file | \
sed -e '1s/^/INSERT INTO file VALUES (/; $!s/$/,/; $s/$/);/'

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:
Code:
for file in /xxx/sss/*
do
    sed -e '1,2d; $d; /^[:space:]*$/d' ${file} | \
    sed -e '1s/^/INSERT INTO file VALUES (/; $!s/$/,/; $s/$/);/'
done

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)
Code:
for file in /xxx/sss/*
do
    filename=$( basename $file )
    sed -e '1,2d; $d; /^[:space:]*$/d' ${file} | \
    sed -e '1s/^/INSERT INTO '${filename}' VALUES (/; $!s/$/,/; $s/$/);/'
done

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.

Last edited by pludi; 02-11-2011 at 10:25 AM..
# 9  
Old 02-11-2011
Hi Pludi,

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/$/);/''
# 10  
Old 02-11-2011
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)?
# 11  
Old 02-11-2011
Its simple actually,

I have n number .sql files under a folder each file is having columns inside the file.

and i save this your code and running on that folder

Code:
for file in /home/db2inst1/test/JAVA/DDL/*
do
filename=$( basename $file )
sed -e '1,2d; $d; /^[:space:]*$/d ${file} \
sed -e '1s/^/INSERT INTO '${filename}' VALUES(/; $!s/$/,/; $s/$/);/'
done


when i ran the file using ./column.sh cmd.its giving error.Once its get successed i will use this part of the code in ohter script.

Code:
./column.sh: line 14: syntax error near unexpected token `('
./column.sh: line 14: ` sed -e '1s/^/INSERT INTO '${filename}' VALUES(/; $!s/$/,/; $s/$/);/''

# 12  
Old 02-11-2011
Ah, I see the error, typo on my part. On the first sed command I've left out the closing quote. The line should read
Code:
sed -e '1,2d; $d; /^[:space:]*$/d' ${file} | \


Last edited by pludi; 02-11-2011 at 10:26 AM..
# 13  
Old 02-11-2011
Hi pludi,

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
Code:
--------------------,
AAAAAAA,
BBBBBBB,
CCCCCCC ,
3 record(s) selected.,
sed: can't read sed: No such file or directory
--------------------,
AAAAAAA,
BBBBBBB,
CCCCCCC....
......
......
.......

21 record(s) selected.,

do we need to edit some thing else? i could not seen any INSERT statement or braces "( ) " printed.
# 14  
Old 02-11-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Reading flat file content

is there any unix tools that can read the text files like through SQL queries? (ie in Hadoop, Impala DB support flat file query) (1 Reply)
Discussion started by: omykarshan
1 Replies

2. UNIX for Beginners Questions & Answers

How to get script to create a new file that lists folder content sorted by size?

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)
Discussion started by: Braveheart
4 Replies

3. Shell Programming and Scripting

Trouble reading content of file from a variable

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)
Discussion started by: Ravindra Swan
9 Replies

4. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

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)
Discussion started by: pkabali
6 Replies

5. Shell Programming and Scripting

Formatting Report and Reading data and fetching the details from contents file

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)
Discussion started by: rameshds
0 Replies

6. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

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)
Discussion started by: mukesh.baranwal
3 Replies

7. Shell Programming and Scripting

Reading several files and summing their content line-by-line

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)
Discussion started by: ergy1983
7 Replies

8. Shell Programming and Scripting

Problem in reading a file content

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)
Discussion started by: garman
8 Replies

9. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

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)
Discussion started by: nimu1979
6 Replies

10. Shell Programming and Scripting

reading a file name as soon as that files arrives into a folder

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)
Discussion started by: dtazv
6 Replies
Login or Register to Ask a Question