Need help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help
# 1  
Old 01-26-2007
Need help

I have a problem. I have a direcotry where i have files named as below

-rwxr-x--- 1 sadalbid dwro 953 Jan 4 17:03 tables.telg.subs.txt
-rwxr-x--- 1 sadalbid dwro 79 Jan 4 17:03 tables.telg.sub2.txt
-rw-r--r-- 1 sadalbid dwro 268 Jan 4 17:03 tables.telg.prty.txt
-rwxr-x--- 1 sadalbid dwro 349 Jan 4 17:03 tables.telg.prod.txt
-rwxr-x--- 1 sadalbid dwro 45 Jan 4 17:03 tables.telg.addr.txt

I have to loop thruogh these text files and get the table name which is the first field in the file. for e.g cat on one of the files above will produce o/p like this

cat tables.telg.subs.txt

acct STD
acct_chrg_off_actvt STD
acct_crdt_hist STD
acct_sgmnt_hist HST
acct_sts_hist HST
acct_upd STD
acct_upd2 STD
acct_inv_due_dt STD
cllctn_agnc_avt AVT
cpni_srv_hist HST
cpni_sts_avt AVT
cpni_sts_hist HST

I have to take the first column from each file here which is the table name and replace in another file which deletes the data from all these tables. How can i loop through all the files and replace the table name in my actual script to delete the data from the tables in these files only. Please suggest.

My delete script looks like this; I want to make it automated so that i don't need to run it manually.

delete from acct;
.IF ERRORCODE <> 0 Then .QUIT 1
delete from acct_chrg_off_actvt ;
.IF ERRORCODE <> 0 Then .QUIT 2
# 2  
Old 01-26-2007
Use ksh? Try this:
Code:
integer i=0
cut -d" " -f1 tables.* | while read table; do
  i=i+1
  echo "delete from $table;"
  echo ".IF ERRORCODE <> 0 Then .QUIT $i"
done > outfile

# 3  
Old 01-26-2007
Problem with cut

Glenn,

Cut command is not working properly as if i do this

cut -d" " -f1 tables.*


the o/p is coming like this:

acct STD
acct_chrg_off_actvt STD
acct_crdt_hist STD
acct_sgmnt_hist HST
acct_sts_hist HST
acct_upd STD
acct_upd2 STD

only the first field which is table name should come right. Please help.
# 4  
Old 01-26-2007
Glenn's solution is much nicer than mine (I got stuck in sed mode) but I present it anyway:

for FILE in *
do
sed -e = $FILE | sed -e N -e "s/\([0-9]*\)\(.\)\([^ \t]*\).*$/delete from \3;\2.IF ERRORCODE <> 0 Then .QUIT \1/"
done


There is no right or wrong -- it's UNIX -- there's just style.

Last edited by hegemaro; 01-26-2007 at 01:13 PM.. Reason: cleaner code and checks for either tab or space per anbu23
# 5  
Old 01-26-2007
Quote:
Originally Posted by dsravan
Glenn,

Cut command is not working properly as if i do this

cut -d" " -f1 tables.*


the o/p is coming like this:

acct STD
acct_chrg_off_actvt STD
acct_crdt_hist STD
acct_sgmnt_hist HST
acct_sts_hist HST
acct_upd STD
acct_upd2 STD

only the first field which is table name should come right. Please help.
Is that a space after the first field, or something else like a tab?
# 6  
Old 01-26-2007
check whether the input file contain tab as delimiter
# 7  
Old 01-26-2007
It's space for some fields and tab+space for some fields. what is the best soultion in this scenario. Is it cut or SED.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question