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 Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-16-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
There's one more bump in the code , the increment of n in the loop should be after the echo statement.

Regards
  #2 (permalink)  
Old 06-16-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Quote:
Originally Posted by Franklin52 View Post
There's one more bump in the code , the increment of n in the loop should be after the echo statement.

Regards

Hi guys.. thanks the code is running.
thanks all
Regards.

-----Post Update-----

Quote:
Originally Posted by Anteus View Post
yes..Sorry
tY is not there.

so substituted in place of tY .. de0 in the frankiln52's code.
i.e. and also added some echos to see what values are going into the array..

Code:
 
IFS=""
event=`psg proc_s | grep de0`
for line in $event
do
  echo $line
done
 
 
n=0
psg proc_s | awk '/de0/{split($5,a,":");print a[2]}'| while read s
do
  Sam[$n]="$s"
  n=$(( $n + 1))
  echo ${Sam[$n]}
  echo ${Sam[0]}
   echo ${Sam[1]}
   echo ${Sam[2]}
   echo ${Sam[3]}
   echo ${Sam[4]}
    echo ${Sam[5]}
  # other code
  # ....
done
echo "time is ${Sam[5]}"
echo $n
and got the output as five blank lines..
echo ${Sam[$n]} seems to be printing blank lines.

Output:

crmcint 15326 1 0 14:16:27 ? 0:01 proc_s de081.c
crmcint 16428 1 0 14:35:49 ? 0:00 proc_s de08.c
crmcint 10272 1 0 13:12:48 ? 0:06 proc_s de01.c
crmcint 20229 1 0 15:06:23 ? 0:00 proc_s de05.c
crmcint 18285 1 0 14:51:38 ? 0:00 proc_s de083.c
crmcint 18902 1 0 14:57:02 ? 0:01 proc_s de083p.c
...
Hello

is it possible in the above code to get the number of splits that have happened over her..
suppose if the above code goes like this

psg proc_s |awk -F" " '{print all fields }''| while read s
do
n=$(( $n + 1))
Sam[$n]="$s"
done

now in case if i do echo ${#Sam[0]} it gives number of charachters in the Sam[0] array..what if i have to find number of words...i.e to get the
number of spilts per line..how can it be done
  #3 (permalink)  
Old 06-16-2009
tkleczek tkleczek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 34
If I understand you correctly, the following code should do the trick:
Code:
echo "${Sam[0]}" | awk '{ print NF }'
  #4 (permalink)  
Old 06-17-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Quote:
Originally Posted by Anteus View Post
now in case if i do echo ${#Sam[0]} it gives number of charachters in the Sam[0] array..what if i have to find number of words...i.e to get the
number of spilts per line..how can it be done
The split function returns the number of the elements of the array:

Awk - A Tutorial and Introduction - by Bruce Barnett

Regards
  #5 (permalink)  
Old 06-17-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
psg ksh | awk '{ printf("pid=%d uid=%s command=%s\n", $2, $1, ($5 ~ /^[A-Z]/)? $9:$8) }'
  #6 (permalink)  
Old 06-17-2009
Anteus Anteus is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Quote:
Originally Posted by vgersh99 View Post
Code:
psg ksh | awk '{ printf("pid=%d uid=%s command=%s\n", $2, $1, ($5 ~ /^[A-Z]/)? $9:$8) }'

hi...
can u explain what is $5 ~ /^[A-Z]/)? $9:$8) .. i cannot understand that.

one more thing can i modify this code like this

psg ksh | awk '{
pid=$2
uid=$1
command=($5 ~ /^[A-Z]/)? $9:$8) }'

thanks
  #7 (permalink)  
Old 06-17-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by Anteus View Post
hi...
can u explain what is $5 ~ /^[A-Z]/)? $9:$8) .. i cannot understand that.
If the fifth field starts with an upper case letter, return the value of the ninth field, otherwise return the value of the eight field.
Quote:
Originally Posted by Anteus View Post
one more thing can i modify this code like this

psg ksh | awk '{
pid=$2
uid=$1
command=($5 ~ /^[A-Z]/)? $9:$8) }'

thanks
i don't see why not - would not hurt to try it.
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 11:39 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