Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

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

Occasional Advisor

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.
8 REPLIES
Acclaimed Contributor Acclaimed Contributor

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

I assume you want to take all of the names on the left of the ".", and sort -u them.
Then take all of the names on the right and do the same thing. Then put out those two lines?

I assume you don't care about the order of the lists of the first two lines and that you may output:
SCHEMAS=OWNER1,OWNER
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"

And that the total number of lines in the input files is S * I?
Honored Contributor Honored Contributor

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

>> Please let me know there are any questions.

Yeah, why not a perl or awk solution?
This is not a shell or scripting task, it is word processing task.

In perl it could look like

$cat x.txt
OWNER.TABLE_NAME
OWNER.TABLE_NAME1
OWNER1.TABLE_NAME
OWNER1.TABLE_NAME1
$cat x.pl
use strict;
my (%owners, %tables);
while (<>) {
chomp;
my ($o,$t) = split /\./;
$owners{$o}++;
$tables{$t}++;
}
my $owner_list = join q(,), sort keys %owners;
my $table_list = join q(','), sort keys %tables;

print <<"EOF";
SCHEMAS=$owner_list
INCLUDE=TABLE:"IN ('$table_list')"
EOF

$perl x.pl x.txt
SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"

Cheers!
Hein.


Acclaimed Contributor Acclaimed Contributor

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

Here is a script that uses awk, sort and the shell to produce your output:

TMP=/var/tmp/itrc.sc.$$
awk -F. -v OUT2=${TMP}b '
{
print $1
print $2 >> OUT2
} ' input-file | sort -u > ${TMP}a

sort -u -o ${TMP}b ${TMP}b

IFS_SAVE=$IFS

set -A SCH -- $(< ${TMP}a )
IFS=","
echo "SCHEMAS=${SCH[*]}"

IFS=$IFS_SAVE
set -A SCH -- $(< ${TMP}b )
echo "INCLUDE=TABLE:\"IN ('\c"

(( i = 0 ))
(( j = ${#SCH[*]} ))
while [ $i -lt $j ]; do
echo "${SCH[i]}\c"
(( i = i + 1 ))
if [ $i -lt $j ]; then
echo "','\c"
fi
done

echo "')"\"

rm -f ${TMP}a ${TMP}b
Occasional Advisor

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

Hi,

Thanks a lot for your response...
Yes, you are correct.. Order is not a problem.
I will try with the awk script and let you if there are any issues.
In the script, i am not using the perl...

Thanks!
bsraj.
Valued Contributor Valued Contributor

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

how about this?

T1=`cat xxx|cut -d\. -f1|sort -u`
echo $T1| tr " " "," | \
sed 's|^|SCHEMAS=|g'
T1=`cat xxx|cut -d\. -f2|sort -u`
echo $T1| tr " " ","|sed \
-e"s|^|\'|g" \
-e"s|$|\'|g" \
-e"s|^|INCLUDE=TABLE:\"IN \(|g" \
-e"s|$|\)\"|g"



If it ain't broke, let me have a look at it.
Honored Contributor Honored Contributor

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

do all owners each have the same tables, or is it possible for some owners to have tables the others dont?

is it acceptable to generate a file that *says* all owners have all tables?

for example

O.T
0.T1
01.T
01.T1
02.T3

should this say

SCHEMAS=0,01,02
INCLUDE=TABLE:"IN('T','T1','T2')"

or

SCHEMAS=0,01
INCLUDE=TABLE:"IN('T','T1')"
SCHEMAS=02
INCLUDE=TABLE:"IN('T3')"
Occasional Advisor

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

Hi Steve Faidley,

Thanks for your response..
I am not sure how to modify ur code to get single codes after all the tables..
Could you please hele me to get that..

The Output from the same code.

SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME,TABLE_NAME1')"


I like to get single codes after every table.

SCHEMAS=OWNER,OWNER1
INCLUDE=TABLE:"IN ('TABLE_NAME','TABLE_NAME1')"

Please let me know there are any quesitons.

Thanks,
bsraj.
Highlighted
Occasional Advisor

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

Hi OldSchool,

Again thanks..
Yes, i need to make the output file as your first like guess.

should this say

SCHEMAS=0,01,02
INCLUDE=TABLE:"IN('T','T1','T2','T3')"

I have added the table T3 in the output..

Please let me know there are any quesitons.


Thanks,
bsraj.