If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > awk used in for loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-04, 09:11
abe6162 abe6162 is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
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
Reply With Quote
  #2 (permalink)  
Old 02-24-04, 09:27
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
If you doesn't specify a file, 'awk' read stdin, are you sure that is what you want to do ?
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-24-04, 09:29
abe6162 abe6162 is offline
Registered User
 
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


Quote:
Originally posted by aigles
If you doesn't specify a file, 'awk' read stdin, are you sure that is what you want to do ?
Reply With Quote
  #4 (permalink)  
Old 02-24-04, 09:48
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #5 (permalink)  
Old 02-24-04, 10:08
abe6162 abe6162 is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
Thanks that worked!!!

Quote:
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On