retreiving and assigning values and manipulating string in a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retreiving and assigning values and manipulating string in a for loop
# 22  
Old 06-17-2009
Quote:
Originally Posted by tkleczek
If I understand you correctly, the following code should do the trick:
Code:
echo "${Sam[0]}" | awk '{ print NF }'

Hi I have used the tip u gave and its working..

its something like this

Code:
psg ksh |awk '{ print NF }' | while read s
 do
       var[$n]="$s"
       echo ${var[$n]}
       n=$(( $n + 1))
 done

it gives me the number of records in each line..

but as a learner i am unable to understand the flow of input to the awk command..as i have refernced here http://www.grymoire.com/Unix/Awk.html#uh-17 but its not working out..where i am missing out..

Code:
psg ksh |awk '{ print NF }' | while read s
 do
       (if i have to access the (output of psg ksh here) which i need inorder to calculate the length of a field)
        print $8 (it gives me a blank line.)
       
 done



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

Another way i tried is like this
Code:
psg ksh |awk -f
  BEGIN 
  {
  n=split(/*how the output line of ksh will be directed here */,arr," ");
  for (i=1;i<=n;i++) 
    {
      echo print ${arr[i]}
      echo length ${arr[i]}
    }
 }


Last edited by Anteus; 06-17-2009 at 03:55 AM..
# 23  
Old 06-17-2009
Quote:
Originally Posted by Anteus
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
# 24  
Old 06-17-2009
Quote:
Originally Posted by Franklin52
The split function returns the number of the elements of the array:

Awk - A Tutorial and Introduction - by Bruce Barnett

Regards

Hi..

i worte the code as per the tutorial..

Code:
 
psg ksh |awk -f
BEGIN {
     n=split($0,arr," ")
    for (i=1;i<=n;i++)
    {
      echo print ${arr[i]}
      echo length ${arr[i]}
    }
  }

it gives an error like like this

Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...
Test[27]: BEGIN: not found
Test[27]: syntax error at line 28 : `(' unexpected
# 25  
Old 06-17-2009
Quote:
Originally Posted by Anteus
Hi..

i worte the code as per the tutorial..

Code:
 
psg ksh |awk -f
BEGIN {
     n=split($0,arr," ")
    for (i=1;i<=n;i++)
    {
      echo print ${arr[i]}
      echo length ${arr[i]}
    }
  }

it gives an error like like this

Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...
Test[27]: BEGIN: not found
Test[27]: syntax error at line 28 : `(' unexpected
Remove the -f option in your statement, the example of the tutorial is an awk script.

Regards
# 26  
Old 06-17-2009
Quote:
Originally Posted by Franklin52
Remove the -f option in your statement, the example of the tutorial is an awk script.

Regards

Code:
 
psg ksh | awk BEGIN { n=split($0,arr," ")for (i=1;i<=n;i++)
       {
        echo print ${arr[$i]}
        echo length(${arr[$i]})
       }
}

its showing error like
Tes: syntax error at line 1 : `(' unexpectedSmilie
# 27  
Old 06-17-2009
I am not sure of your requirement. But awk syntax should be like below

Code:
psg ksh | awk ' BEGIN { n=split($0,arr," ") ;  for (i=1;i<=n;i++)
       {
        echo print ${arr[$i]}
        echo length(${arr[$i]})
       }
}'

# 28  
Old 06-17-2009
Quote:
Originally Posted by panyam
I am not sure of your requirement. But awk syntax should be like below

Code:
psg ksh | awk ' BEGIN { n=split($0,arr," ") ; for (i=1;i<=n;i++)
       {
        echo ${arr[$i]}
        echo length(${arr[$i]})
       }
}'

i did that it shows diff error
syntax error The source line is 4.
The error context is
echo >>> ${ <<<
awk: The statement cannot be correctly parsed.
The source line is 4.
syntax error The source line is 5.

when i do like this echo "${arr[$i]}" nothing prints. and plus i have a same parse error at next line that is at echo length(${arr[$i]})...

y is this happening .. in earlier program i used same method to print my values in arrays.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

2. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

3. UNIX Desktop Questions & Answers

Manipulating a list of numbers based on values

Hi, I have a single column of numbers from in tabulated text format ranging from 0 to 1. I want to manipulate the list of numbers so that if the number is greater than 0.5 (> 0.5), I get 1 - number. If the number is less than 0.5, the number is taken as it is and not altered. For example: ... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

5. Shell Programming and Scripting

Need help in manipulating string variable

I have written a shell script to do some processing and have to manipulate a variable. Basically, the variable is like this -- var=set policy:set cli My purpose is to split it into two variables based on the position of ":". To get the right end, I am doing this -- vaa1=${vaa#*:} ... (1 Reply)
Discussion started by: Dev_Sharma987
1 Replies

6. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

7. Shell Programming and Scripting

Assigning the values to an Array

hi every body, i donot know how to assign a array varible with a file see i having file more file property1 Name property2 Address the above two line are tab Space seperated between the property and its value i want to seperate it and assign to... (1 Reply)
Discussion started by: kkraja
1 Replies

8. UNIX for Dummies Questions & Answers

string manipulating

I have a file like this: Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 6 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 2 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 5 With this command seira=`cut -f 2 -d ';' tes.txt` i take all the results (6,2,5 etc) and i store them in variable seira When i do... (9 Replies)
Discussion started by: psalas
9 Replies

9. UNIX for Dummies Questions & Answers

assigning values to a variable

i try to get the year and month values using the below shell script when i enter the script like this #!/usr/bin/ksh dd=`DATE +%Y%M` echo $dd it is showing the error as shown below abc.ksh: DATE: not found any suggestions please (3 Replies)
Discussion started by: trichyselva
3 Replies

10. UNIX for Dummies Questions & Answers

Assigning values to an array

The way I've been using arrays currently have been: #!/bin/ksh set -A myArray myArray=value1 myArray=value2 myArray=value3 myArray=value4 Is there a way I can assign values to an array that will automatically place the value into the next element in the array like: myArray=value1... (4 Replies)
Discussion started by: yongho
4 Replies
Login or Register to Ask a Question