That works.  But I really do want the end result in a variable.  I  
could redirect the output from the awk pipeline into a variable (e.g.  
file=$(...) ) but forking processes seems like overkill.  But your  
suggestion which uses awk's auto-split feature did get me thinking  
about using an array in bash:

echo foo.bar.txt |
while IFS=. read -a file ; do
   echo ${file[1]}               # the second field
   echo ${line[${#line}-2]}      # the second to last field
done

Although I'm still wondering if there might be some other solutions.   
Ideally, I'm looking for something similar to perl's split or s///  
features but from within bash.  I was thinking that 'set' might work,  
but didn't have any luck:

This works:
( set foo bar txt ; echo $1 )

This doesn't:
( IFS=. set foo.bar.txt ; echo $1 )

Regards,
- Robert

On May 17, 2006, at 5:51 PM, Eric Wilson wrote:
> How about this one:
>
> [EMAIL PROTECTED]:~$ cat filename
> foo.bar.txt
> joe.bar.junk
> www.big.bad.bar.goo
>
> ------   awk's NF var is Number of fields ---
> ------   this would always spit out the second to the last field. ---
>
>
> [EMAIL PROTECTED]:~$ cat filename  | \
>> while read FILE
>> do
>> echo $FILE | awk -F'.' '{print $(NF-1)}'
>> done
> bar
> bar
> bar
>
> Cheers;
>
> E!
>
>> Anyone have an alternate way to parse an environment variable?
>>
>> I have a filename in an environment variable that I'd like to parse.
>> The filename is of the form "foo.bar.txt" and I'd like to parse out
>> "bar".  This is the solution I'm currently using:
>>
>> file=foo.bar.txt
>> file=${file%.txt}
>> file=${file#foo.}
>>
>> Any other solutions?

 
_______________________________________________
CWE-LUG mailing list
[email protected]
http://www.cwelug.org/
http://www.cwelug.org/archives/
http://www.cwelug.org/mailinglist/

Reply via email to