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
# 1  
Old 02-11-2011
Reading files under a folder and formatting content of each file

I have 'n' number of files in a folder .each file in the folder "myfolder" is having the content like.
Code:
COLNAME
------------
AAAAAA
BBBBBB
CCCCCC
DDDDDD
...
...

...
ZZZZZZ
26 recrod(s) selected.


My request is by reading each file in "myfolder" and format each file such a way delete line "COLNAME","------" ,"26 records(s) selected" including the blank lines.Hence i need an out like.

Code:
AAAAA
BBBBB
CCCCC
DDDDD
....

....

ZZZZZZ

Note:

This should be for all the files in a folder call "myfolder".after formating . i need to print .

Code:
INSERT INTO<file-name> (AAAAA,
BBBB,
CCCC,
DDDD,
...........
......
zzzzzz

Can somebody help me on this ?
# 2  
Old 02-11-2011
What have you tried so far, and where are you stuck?
# 3  
Old 02-11-2011
I don't know how to read files in folder .thats why i put a mail.

i was just tying on file content.using awk.

awk '{print $1}' 1.txt

Its giving out put with blankspaces as well.Please suggest how to start at least.
# 4  
Old 02-11-2011
Looping over all files in a folder is as simple as
Code:
for file in /path/to/directory/*
do
    # Do something with $file here
done

Other than that I'd say you'll need basename and sed
# 5  
Old 02-11-2011
Yes pludi,

I started using this code and written like

Code:
for fname in  `ls /xxx/sss`
do                                                                                                  do
cat $fname|awk '{print $1}'
done

out put is coming like

Code:
coluumname
---------
AAAAA
BBBBB
CCCCC
....

4

Columnname
----------
AAAAAA
BBBBBB
CCCCCC
....

FFFFF

5...

from this oupt put i need to delete the blankspaces and line "columname" line which has 4 ,5 ...etc.

Can you suggest how to add negetive condition in awk to not prient specifi lines?
# 6  
Old 02-11-2011
First, there's no need to invoke ls for the loop, the shell can expand the parameters just fine on itself. Second, for removing certain lines I've found sed to do a much better job:
Code:
for file in /xxx/sss/*
do
    sed -e '1,2d;$d' $file
done

# 7  
Old 02-11-2011
Thanks pludi.

I trid

out put should come like .
Code:
for fname in  `ls /xxx/sss`
do                                                                                                  do
cat $fname|awk '/./''{print $1}'
done


even sed is working .But how to achieve the below output.any suggession please?


Code:
INSERT INTO FILENAME1 (AAAA,BBB,CCC);
INSERT INTO FILENAME2 (AAAA,BBBB,CCCC,DDD,FFF);

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