The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn shell and awk question mastachef Shell Programming and Scripting 6 10-31-2007 07:15 AM
korn shell question mich_elle Shell Programming and Scripting 4 02-22-2006 09:03 PM
Korn Shell Coprocess Performance Question Mark Puddephat Shell Programming and Scripting 8 12-14-2005 05:33 PM
AWK question in the KORN shell penfold Shell Programming and Scripting 11 02-09-2005 09:14 AM
Question about Korn Shell Latha Nair Shell Programming and Scripting 13 11-05-2003 07:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-08-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
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 (permalink)  
Old 12-08-2005
mona's Avatar
mona mona is offline
Registered User
  
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
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 (permalink)  
Old 12-09-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Thank you very much mona!!
That's exactly what I wanted!!
BTW do you know any good sites for learning shell programming?
  #4 (permalink)  
Old 12-09-2005
aniseed's Avatar
aniseed aniseed is offline
Registered User
  
 

Join Date: Dec 2005
Location: Mumbai, India
Posts: 23
This page has several links to good sites.
http://stommel.tamu.edu/~baum/programming.html
  #5 (permalink)  
Old 12-09-2005
aniseed's Avatar
aniseed aniseed is offline
Registered User
  
 

Join Date: Dec 2005
Location: Mumbai, India
Posts: 23
Additionally, you could check this link for Korn shells specifically.
http://www.unix.org.ua/orelly/unix/ksh/
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0