On 2016-04-27, Marek Novotny <
marek....@marspolar.com> wrote:
> On 2016-04-27,
cnu...@yahoo.com <
cnu...@yahoo.com> wrote:
>>
>> I want to process lines from a file. My file has password
>> entries and one field has a blank so that is treated as a new
>> line. How do I treat each line as a whole ?
>>
>> name_list
>> js3712:x:1501:1501:Joe Smith:/home/js3712:/bin/bash
>> bb238w:x:1502:1502:Bill Brown:/home/wb238w:/bin/bash
>> dq8057:x:1503:1503:Don Quioxte:/home/dq8057:/bin/bash
>>
>> for user in `cat name_list`
>> do
>> echo $user
>> done
>>
>> Gives:
>> js3712:x:1501:1501:Joe
>> Smith:/home/js3712:/bin/bash
>> [...]
>
> Change the in field separator.
>
> IFS=$'\n'
>
> Put that on top of your code and try it again.
"$''" is a ksh-ism. The portable version would be
IFS='
'
But, typically, "for var in" is used for iterating on words. To
iterate on lines, "while read" is more common, as in BitTwister
@
mouse-potato.com's reply. It has the advantage of not bombing
if name_list ever grows beyond ARG_MAX bytes long and it frees
up IFS to get to the individual fields :
while IFS=: read -r fld1 fld2 tail; do
printf '%s\t%s\n' "$fld2" "$fld1"
done <name_list
--
André Majorel
http://www.teaser.fr/~amajorel/
J'ai des vrais problèmes, vous avez des faux problèmes.