Reading line by line from unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading line by line from unix script
# 1  
Old 03-22-2010
Reading line by line from unix script

Hi

I am a complete newbie in unix. Learning the ropes. I have a task where I have to write a shell script to read a file line by line. I have tried some code from the net like.

The file data looks like this.

FIN427;2010003;2010003
FIN475;2010001;2010003
FIN476;2010001;2010003
FIN474;2010001;2010003

I want the script to read the parm file one by one and result should like this:

DELETE FROM FINDBT40.FIN427 where fiscper between 2010003 and 2010003;

DELETE FROM FINDBT40.FIN475 where fiscper between 2010001 and 2010003;

DELETE FROM FINDBT40.FIN476 where fiscper between 2010001 and 2010003;

DELETE FROM FINDBT40.FIN474 where fiscper between 2010001 and 2010003;
# 2  
Old 03-22-2010
Code:
awk -F';'  '{ printf("DELETE FROM FINDBT40.%s where fiscper between %s and %s\n",
              $1, $2, $3) }'  inputfile > outputfile.sql

# 3  
Old 03-22-2010
Not quite what I need.

Thanks for such a quick reply. What I am trying to do is put the parameter into the delete sql and run it. I am using the BTEQINF file to run the sql.

So my question is, how can I read the paramter value from fiscper.parm and run the DELETE SQL with correct tablename and number values.

Here is what I wrote:



Code:
while read -r tbl_name st_dt en_dt
do
     tbl_name=`cut -d";" -f1 /transfer/sap_fin/sap_rsync/cca/fiscper.parm`
     st_dt=`cut -d";" -f2 /transfer/sap_fin/sap_rsync/cca/fiscper.parm`
     en_dt=`cut -d";" -f3 /transfer/sap_fin/sap_rsync/cca/fiscper.parm`
     /upapps/inf/dev/scripts/bteqinf > /upapps/inf/dev/scripts/sap_bi_fin/findbt40;
     'DELETE FROM FINDBT40."$tbl_name" where fiscper between "$st_dt" and "$en_dt";'
     /upapps/inf/dev/scripts/bteqinf <<-!
done

What this script is doing is putting all the first column values into one

something like

DELETE FROM FIN427
FIN475
FIN474
FIN 476
where fiscper between 2010003
2010001
2010001
2010001

and 2010003
2010003
2010003
2010003


and I want it to be able to run the sql when I run the script.

DELETE FROM FINDBT40.FIN427 where fiscper between 2010003 and 2010003;

DELETE FROM FINDBT40.FIN475 where fiscper between 2010001 and 2010003;

DELETE FROM FINDBT40.FIN476 where fiscper between 2010001 and 2010003;

DELETE FROM FINDBT40.FIN474 where fiscper between 2010001 and 2010003;

edit by bakunin: please note that code-tags around code are a free-of-charge add-on of this forum. Don't be shy to use them yourself from now on.

Last edited by bakunin; 03-22-2010 at 05:24 PM..
# 4  
Old 03-22-2010
Code:
sed 's/;/ /g;s/^\(.[^ ]*\)\(.[^ ]*\)\(.[^ ]*\)/DELETE FROM FINDBT40.\1 where fiscper between \2 and \3/' file

# 5  
Old 03-22-2010
Thanks!

Hi,

Can you explain what the command doing?

Code:
sed 's/;/ /g;s/^\(.[^ ]*\)\(.[^ ]*\)\(.[^ ]*\)/DELETE FROM FINDBT40.\1 where fiscper between \2 and \3/' file

edit by bakunin: the same as said above goes here. Thank you for your cooperation.

Last edited by bakunin; 03-22-2010 at 05:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. UNIX for Dummies Questions & Answers

UNIX Help for reading line

Hi there, I am trying to read the content such that if there is 'adsmsext.dll', it will read everything from line one until it reads line 6 with 'C:' then it will terminate at line 5. 1. C:\WINDOWS\system32\adsmsext.dll NT AUTHORITY\Authenticated Users:R 2. ... (5 Replies)
Discussion started by: alvinoo
5 Replies

3. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

4. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. Shell Programming and Scripting

Reading a line and the next line in a shell script

Hi, I'm trying to read a line and the next line in a shell script by executing the following code: for i in `seq 1 $numLines`; do line=$(sed -n '{$i}p' outputFile) echo $line done $numLines contanis the outputFile's number of lines. It doesn't work and I cannot... (8 Replies)
Discussion started by: JuanPerez
8 Replies

6. Shell Programming and Scripting

Reading line1 with line2 and line 2 with line 3 in unix

Hi, I have a requirement where i have to read a file line by line and see if the string(fixed postion 10 to 15 in the file) in line 2 is greater than string in line 1. I have used following code while read LINE1 do current_inv_no=$(echo "$LINE1" | cut -c 10-15) read... (2 Replies)
Discussion started by: chethanbg2010
2 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

9. UNIX for Dummies Questions & Answers

reading a line into a variable in unix

hi... i need to open a file and read it line by line, and capture that it in to some variable and manipulate... i need to get a line in to a variable please help :confused: (1 Reply)
Discussion started by: lmadhuri
1 Replies
Login or Register to Ask a Question