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
Assigning values to an array via for/while loop fiori_musicali Shell Programming and Scripting 2 11-24-2008 11:01 PM
Assigning the values to an Array kkraja Shell Programming and Scripting 1 08-11-2008 06:28 AM
string manipulating psalas UNIX for Dummies Questions & Answers 9 04-15-2008 10:00 AM
assigning values to a variable trichyselva UNIX for Dummies Questions & Answers 3 12-14-2007 01:55 AM
Assigning values to an array yongho UNIX for Dummies Questions & Answers 4 07-13-2005 08:49 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-15-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
retreiving and assigning values and manipulating string in a for loop

Hi

I am new to shell scripting and i am preparing a script.
for now i am work on a sub part of it..but i am unable to make it work.

---
the test code that i am working on
--------------------------
IFS=""
Sample_eve=`psg proc_s | grep tY`
n=0
for line in $Sample_eve
do
n=`expr $n + 1`
Sam$n=$(`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`)
echo $Sam$n
done
----------------------------
what i am trying to this is..
list all the process named proc_s that are currently running and are on the machine tY..
populate them in the variable Sample_eve.
and then access each line from the Sample_eve and process them on an individual basis.
but it is not happening ..for loop only goes through once..if i do not set IFS="" then line takes word by word data from the $Sample_eve...by which i cannot get seperate start time for each process. (it runs 35 times for 4 lines..i want to make it run only 4 time for 4 line).

i am doing `echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'` ..so as to get the start time for each process in minutes..then i am assigning it into array Sam so as to get the start Minute for each process.
but getting an output error like this..
for this array assignment as Test[9]: 04^J28^J04^J09: not found.
04, 28,04,09 are correctly shown they are the minutes ..but is not in a proper manner.

i am stuck with this thing .. please help me. I am unable to make the code work.

i am using the korn shell.
thanks
  #2 (permalink)  
Old 06-15-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 375
Quote:
Originally Posted by Anteus View Post
Sam$n=$(`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`)
You can not do like this... $ should not come on the left side.
This will work...
Code:
Sam=`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`
  #3 (permalink)  
Old 06-15-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Quote:
Originally Posted by rakeshawasthi View Post
You can not do like this... $ should not come on the left side.
This will work...
Code:
Sam=`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`


-----------

Hi thanks.. the code you gave worked.

but further while accessing the array Sam. i am having some issues.

1: When i echo ${Sam[$2]} rather than giving the 2nd element it prints all of them

2: for (( i = 0 ; i < ${#Sam[@]} ; i++ ))
do
echo ${Sam[$i]}
done
its throwing an error
Test[16]: syntax error at line 17 : `((' unexpected

can you please tell how to go about accessing the array..
  #4 (permalink)  
Old 06-15-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 375
Quote:
Originally Posted by Anteus View Post
-----------

Hi thanks.. the code you gave worked.

but further while accessing the array Sam. i am having some issues.

1: When i echo ${Sam[$2]} rather than giving the 2nd element it prints all of them

2: for (( i = 0 ; i < ${#Sam[@]} ; i++ ))
do
echo ${Sam[$i]}
done
its throwing an error
Test[16]: syntax error at line 17 : `((' unexpected

can you please tell how to go about accessing the array..
We can not write a for loop like this, unless you are using awk.
I am giving an example of Array and for loop for you...
Code:
set -A _Array 1 2 3 4 5
for i in ${_Array[@]}
do
   echo $i
done
  #5 (permalink)  
Old 06-15-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Quote:
Originally Posted by rakeshawasthi View Post
We can not write a for loop like this, unless you are using awk.
I am giving an example of Array and for loop for you...
Code:
set -A _Array 1 2 3 4 5
for i in ${_Array[@]}
do
   echo $i
done

Yeah . but what to do if only the second element of the array Sam has to be echoed..

and why does echo ${Sam[$2]} prints the entire Sam array??
  #6 (permalink)  
Old 06-15-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 375
this way...
Code:
echo ${_Array[1]}
  #7 (permalink)  
Old 06-15-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Quote:
Originally Posted by rakeshawasthi View Post
this way...
Code:
echo ${_Array[1]}

Tried..but its not printing anything...any other way u know to do this.

the test code is

IFS=""
eve=`psg ftp | grep tV`
n=0
for line in $eve
do
n=`expr $n + 1`
echo $line
array=`echo $line |awk -F" " '{print $5}' |awk -F":" '{print $2}'`
echo ${array[2]}
done
Sponsored Links
Reply

Bookmarks

Tags
awk, cut, for each, for loop

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 07:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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