[lug] bash - awk - command substitution - for loop

Chip Atkinson chip at pupman.com
Tue Nov 6 20:37:57 MST 2007


One thing that comes to mind is $IFS, the internal field separator.

Check this out, maybe it'll get you going in the right direction:

http://tldp.org/LDP/abs/html/internalvariables.html

chip

On Tue, 6 Nov 2007, karl horlen wrote:

> I'm sure this is a simple question but I've been
> searching for the last 3 hours and can't solve it.
> 
> I'm trying to feed the output of awk to a for loop.
> 
> The problem is that if the output of awk contains
> multiple fields separated by any whitespace, the
> variable i will be set for each and every field and
> not the entire record.
> 
> As a bogus example:
> 
> for i in $( awk '{print $0}' access_log  | sort)
> do 
> echo $i "
> done
> 
> Each field of the given record is printed on a new
> line.  I want all the fields for a given record to
> passed to $i as one long string.
> 
> If i use echo -n it concatentates all variable i's for
> all records into one massive long string.  No good.
> 
> I've tried wrapping the command substitution in quotes
> as well:
> 
> for i in "`awk '{print $0}' access_log  | sort `"
> do 
> echo $i "
> done
> 
> And i get one huge variable again.  No good.  If I
> take out the quotes, I'm back to each field being on a
> separate line.
> 
> Hopefully you can follow here.  How do I preserve
> white space within each record output generated by awk
> in the command substitution?
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 




More information about the LUG mailing list