Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    24

    Unanswered: awk used in for loop

    I doing a for loop using awk as in below ..

    for x in `awk '{print $1$2}'`


    done

    is there any why to see the what is in the second field inside the for loop. for example ( I tried this but it didnt' work) ..

    for x in `awk '{print $1$2}'`

    awk 'print{$2}' $x

    done

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    If you doesn't specify a file, 'awk' read stdin, are you sure that is what you want to do ?
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    24
    Sorry. I did include a file for awk to read in. for example...

    for x in `awk '{print$1$2}' tst `

    awk '{print$2}' $x

    done


    Originally posted by aigles
    If you doesn't specify a file, 'awk' read stdin, are you sure that is what you want to do ?

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    awk '{print$2}' $x

    In this statement, awk try to print the second field of every lines of the file $x.
    If you want that awk works on the contents of the variable, tou must write :

    echo $x | awk '{print $2}'

    In fact the result will be empty and not the field2 of the original file, since you concatenate fields 1 and 2.

    If tst contains 'aaaa bbbb ccc'
    x will contains "aaaabbbb" which is single field.


    What do you want to do exactly ?
    If you need fields 1 and 2, you can do :
    Code:
    while read field1 field2 other_fields
    do
       echo "Field 1: $field1"
       echo "Field 2: $field2"
    donei < tst
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    24
    Thanks that worked!!!

    Originally posted by aigles
    awk '{print$2}' $x

    In this statement, awk try to print the second field of every lines of the file $x.
    If you want that awk works on the contents of the variable, tou must write :

    echo $x | awk '{print $2}'

    In fact the result will be empty and not the field2 of the original file, since you concatenate fields 1 and 2.

    If tst contains 'aaaa bbbb ccc'
    x will contains "aaaabbbb" which is single field.


    What do you want to do exactly ?
    If you need fields 1 and 2, you can do :
    Code:
    while read field1 field2 other_fields
    do
       echo "Field 1: $field1"
       echo "Field 2: $field2"
    donei < tst

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •