The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to Read Error,jbase,voilation,segmentation words from a file Mujtaba khan Shell Programming and Scripting 3 03-30-2008 10:19 AM
Need to create file from shell scripting smr_rashmy Shell Programming and Scripting 4 01-28-2008 06:59 PM
how to read all the unique words in a text file aditya.ece1985 Shell Programming and Scripting 5 11-29-2007 10:26 PM
FTP is using shell scripts create ? for file a501420038 Shell Programming and Scripting 1 08-16-2007 11:39 AM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 07:28 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-31-2007
Registered User
 

Join Date: May 2007
Posts: 11
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-31-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,199
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.
Reply With Quote
  #3 (permalink)  
Old 06-01-2007
Registered User
 

Join Date: May 2007
Posts: 211
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
Reply With Quote
  #4 (permalink)  
Old 06-01-2007
praveenkumar_l's Avatar
Registered User
 

Join Date: May 2007
Posts: 36
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')"
Reply With Quote
  #5 (permalink)  
Old 06-01-2007
Registered User
 

Join Date: May 2007
Posts: 11
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..
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0