Read words from file and create new file using K-shell.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read words from file and create new file using K-shell.
# 1  
Old 05-31-2007
Read words from file and create new file using K-shell.

Hi All,

Please help me in creating files through K-shell scripts.
I am having one file in this format.
OWNER.TABLE_NAME
OWNER.TABLE_NAME1
OWNER1.TABLE_NAME
OWNER1.TABLE_NAME1

I want to read the above file and create new file through k shell script.
The new file should looks like this.
SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"

Please let me know there are any questions.

Thanks,
bsraj.
# 2  
Old 06-01-2007
A possible solution using awk :
Code:
awk -F'.' -v Q="'" '
   {
      schemas[$1]++;
      include[$2]++;
   }
   END {
      for (s in schemas) 
         schemas_list = schemas_list (schemas_list ? "," : "") s;
      for (i in include)
         include_list = include_list (include_list ? "," : "") Q i Q;
      print "SCHEMAS=" schemas_list;
      print "INCLUDE=TABLE:\"IN (" include_list ")\"";
   }
    ' infile

Jean-Pierre.
# 3  
Old 06-01-2007
Hope this is what you wanted

Code:
#!/bin/ksh

Input_File=$1
Temp_File=temp.txt
Output_File=output.txt
rm -f $Output_File 2> /dev/null

for list_of_users in `cut -d'.' -f1 $Input_File | sort | uniq`
do
        for table_name in `grep -x "^$list_of_users\..*" $Input_File |cut -d'.' -f2`
        do
                tables=$tables","$table_name
        done
        echo $list_of_users":"${tables#,} >> $Temp_File
        tables=""
done
for list_of_objects in `cut -d':' -f2 $Temp_File | sort | uniq`
do
        for user_name in `grep -x "..*$list_of_objects\$" $Temp_File |cut -d':' -f1`
        do
                cmbuser=$cmbuser","$user_name
        done
        echo ${cmbuser#,}":"$list_of_objects >> $Output_File
        cmbuser=""
done
mv $Output_File $Temp_File
for line in `cat $Temp_File`
do
        echo $line | cut -d':' -f1 | read user_list
        echo $line | cut -d':' -f2 | read table_list
        echo "SCHEMAS=$user_list" >> $Output_File
        echo 'INCLUDE=TABLE:"IN('$table_list')"'  >> $Output_File
done
rm -f $Temp_File 2> /dev/null
exit

# 4  
Old 06-01-2007
Quote:
Originally Posted by bsrajirs
Hi All,

Please help me in creating files through K-shell scripts.
I am having one file in this format.
OWNER.TABLE_NAME
OWNER.TABLE_NAME1
OWNER1.TABLE_NAME
OWNER1.TABLE_NAME1

I want to read the above file and create new file through k shell script.
The new file should looks like this.
SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"

Please let me know there are any questions.

Thanks,
bsraj.
Try this. This may not be a generic version.

$ cat file1.ksh
#!/usr/bin/ksh

schemas=$(cut -f1 -d. file1 | sort -u | xargs | sed 's/ /,/')
tables=$(cut -f2 -d. file1 | sort -u | xargs | sed -e 's/ /,/' -e "s/.*/'&/" -e "s/,/','/" -e "s/.*/&'/")

echo "SCHEMAS=$schemas\nINCLUDE=TABLE:\"IN ($tables)\""

$ file1.ksh
SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"
# 5  
Old 06-01-2007
Thanks a lot..

Hi All,

Thank You All!!
I am sure this will help me lot. I am working to test the codes..
I will let you know if there are any issues..
I Appreciate all your help..

Again Thanks to all...

Regards,
bsraj..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell - Read a text file with two words and extract data

hi I made this simple script to extract data and pretty much is a list and would like to extract data of two words separated by commas and I would like to make a new text file that would list these extracted data into a list and each in a new line. Example that worked for me with text file... (5 Replies)
Discussion started by: dandaryll
5 Replies

2. Shell Programming and Scripting

Read Lines from One file.. and create another.. and

Hello Members, I have one file which contains million of supplier code. I need to load these codes into database 1000 at a time. Database procedure reads from an external table which is based on the unix files. All I want to do is to read from the bigger file e.g. MAIN_FILE.txt and create... (1 Reply)
Discussion started by: chetanojha
1 Replies

3. Shell Programming and Scripting

How to create a shell script to read a foldername from a text file and go to the folder?

Hi, I am trying to write a shell script which can read folder names from a text file and then go to the folder and picks up a xml file and write on my sipp script so that I can run the sipp script. For example: I have a text file called thelist.txt where I have provided all the folders... (7 Replies)
Discussion started by: pm1504
7 Replies

4. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

5. Shell Programming and Scripting

Read a file and create egrep variable

I want to create a egrep variable from a file. For example: string=`cat query.txt` cat myfile.txt | egrep "$string" The string variable file has a list of one or multiple lines So the end result of: cat myfile.txt | egrep "$string" would be: cat myfile.txt | egrep... (2 Replies)
Discussion started by: numele
2 Replies

6. Programming

fscanf: read words from file

Hi I have a file like that: 1 2 3 4 5 6 7 8 and I want print on stdout: 1 3 8 in other words i want choose what print out. I was thinking to use fscanf as: fscanf(file_in,"%d %d %d",&a, &b,&c); but in this way i get: 1 2 3 Is there a solution using fscanf to obtain my... (2 Replies)
Discussion started by: Dedalus
2 Replies

7. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

8. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

9. Shell Programming and Scripting

script to Read Error,jbase,voilation,segmentation words from a file

Hi, i need a script to find words "error,jbase,voilation,segmentation" from a file(verify.txt) and if got any of the above words then send an email and if wont find any then execute the cron job. every time it read file verify.txt it must first clear the previous data in the file . Kindly... (3 Replies)
Discussion started by: Mujtaba khan
3 Replies

10. Shell Programming and Scripting

how to read all the unique words in a text file

How can i read all the unique words in a file, i used - cat comment_file.txt | /usr/xpg6/bin/tr -sc 'A-Za-z' '/012' and cat comment_file.txt | /usr/xpg6/bin/tr -sdc 'A-Za-z' '/012' but they didnt worked..... (5 Replies)
Discussion started by: aditya.ece1985
5 Replies
Login or Register to Ask a Question