Korn Shell Loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Loop question
# 1  
Old 12-08-2005
Question Korn Shell Loop question

I'm needing help with assigning variables inside a while loop of ksh script.
I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents.

Code:
$ more file.txt
AAA 001
BBB 002
CCC 003
DDD 004

$ more looptest.ksh
#! /bin/ksh

while read line
do
   awk '{print FS,$1}' >> var1
   awk '{print FS,$2}' >> var2
   
   if [[ $var1 = AAA ]]
   then
           echo group1 $var2
   elif [[ $var1 =  CCC ]]
   then
           echo group2 $var2
    fi 
done < file.txt

The desired output for my ksh script is the below but the section where I assign first and second word to variables isn't working. Could someone tell me how to fix this?

group1 001
group2 003
# 2  
Old 12-08-2005
Code:
awk '{print FS,$1}' >> var1
awk '{print FS,$2}' >> var2

You are redirecting the output to a file and not to a variable. And nowhere you are using the variable "line" in the awk statement. Change the above 2 lines as below and try again.

Code:
var1=`echo $line | awk '{print FS,$1}'`
var2=`echo $line |awk '{print FS,$2}'`

you can also use cut instead of awk to read the first and second word.

Code:
#! /bin/ksh
while read line
do
var1=`echo $line | cut -f1 -d" "` 
var2=`echo $line | cut -f2 -d" "`
   if [[ $var1 = AAA ]]
   then
           echo group1 $var2
   elif [[ $var1 =  CCC ]]
   then
           echo group2 $var2
    fi 
done < file.txt

you can also read the words in the while loop itself

Code:
#! /bin/ksh
while read var1 var2
do
   if [[ $var1 = AAA ]]
   then
           echo group1 $var2
   elif [[ $var1 =  CCC ]]
   then
           echo group2 $var2
    fi 
done < file.txt

# 3  
Old 12-09-2005
Thank you very much mona!!
That's exactly what I wanted!!
BTW do you know any good sites for learning shell programming?
# 4  
Old 12-09-2005
This page has several links to good sites.
http://stommel.tamu.edu/~baum/programming.html
# 5  
Old 12-09-2005
Additionally, you could check this link for Korn shells specifically.
http://www.unix.org.ua/orelly/unix/ksh/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print the missing fields outside the for loop in Korn shell?

I have 2 for loop in my program , first one will list files based on timestamp and second one list the files based on type(RPT / SUB_RPT).Here is my code: #!/bin/ksh STG_DIR=/home/stg for pattern in `find $STG_DIR -type f -name 'IBC*csv' | awk -F'' '{print $(NF-1)}' | sort -u` do echo... (2 Replies)
Discussion started by: ann15
2 Replies

2. Shell Programming and Scripting

Korn Shell Loop Problems

Very new to the Korn Shell, but I've been looking up loops online and it seems this should work. I'm just trying to convert an ip range in variables $A and $B and iterate the individual ip's out to new lines. Unfortunately I get {152..155} instead of 152, 153, 154, and 155. # for i in {$A..$B};... (8 Replies)
Discussion started by: Azrael
8 Replies

3. Shell Programming and Scripting

[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays , The following code works for ctcol in 2 3 4 5 6; do set -A a$ctcol $(cut -d, -f $ctcol test_file) done how ever this does not work for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

4. Shell Programming and Scripting

korn shell remove files question

how do you show each filename in a giving directory and delete the specific file in korn script i was thinking using ls rm ? but i cant make it work (0 Replies)
Discussion started by: babuda0059
0 Replies

5. Shell Programming and Scripting

Help With Constructing A Korn Shell Search Loop

Hello All, I am a statistician and I am very new to the world of ksh programming. Daily, I analyze millions of rows of data and land information to DB2 tables. I have recently been asked to develop a ksh script to FTP an export file containing line item data from the production environment to the... (2 Replies)
Discussion started by: jonesdk5
2 Replies

6. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

7. Shell Programming and Scripting

Korn shell and awk question

I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and... (6 Replies)
Discussion started by: mastachef
6 Replies

8. Shell Programming and Scripting

korn shell question

Hi all, I am trying to tweak my ksh , i am running V: Version M-11/16/88i I have my Backspace and up/down arrows working using the following code in my ~/.profile file. set -o emacs alias __A=$(print '\020' ) alias __B=$(print '\016' ) alias __C=$(print '\006' ) alias __D=$(print... (4 Replies)
Discussion started by: mich_elle
4 Replies

9. Shell Programming and Scripting

AWK question in the KORN shell

Hi, I have two files with the following content: gmrd.txt 235649;03;2563;598 291802;00;2563;598 314634;00;235649;598 235649;03;2563;598 393692;00;2563;598 411805;00;2563;598 411805;00;2563;598 235649;03;2563;598 414037;00;2563;598 575200;00;2563;598 70710;00;2563;598... (11 Replies)
Discussion started by: penfold
11 Replies

10. Shell Programming and Scripting

Question about Korn Shell

In Korn Shell, can you use "go to" statements? Would you then put paragraph names with a colon? For example, would you specify "goto para1" and then have the paragraph with the label para1:? I am getting an error message when Idid this. I have my paragraph name 'clsbooks:' and I get... (13 Replies)
Discussion started by: Latha Nair
13 Replies
Login or Register to Ask a Question