Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

give all lines but ignore the space

36 views
Skip to first unread message

cnu...@yahoo.com

unread,
Apr 27, 2016, 4:58:31 PM4/27/16
to


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
bb238w:x:1502:1502:Bill
Brown:/home/wb238w:/bin/bash
dq8057:x:1503:1503:Don
Quioxte:/home/dq8057:/bin/bash

But I want it to give:
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

Thanks,
Carl

Marek Novotny

unread,
Apr 27, 2016, 5:15:28 PM4/27/16
to
Change the in field separator.

IFS=$'\n'

Put that on top of your code and try it again.

--
Marek Novotny
https://github.com/marek-novotny

Bit Twister

unread,
Apr 27, 2016, 6:03:07 PM4/27/16
to
#!/bin/bash
set -u
_line=""

while read -r _line; do
echo $_line
done < name_list


exit 0

Kaz Kylheku

unread,
Apr 27, 2016, 6:47:29 PM4/27/16
to
On 2016-04-27, cnu...@yahoo.com <cnu...@yahoo.com> wrote:
>
>
> 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
> bb238w:x:1502:1502:Bill
> Brown:/home/wb238w:/bin/bash
> dq8057:x:1503:1503:Don
> Quioxte:/home/dq8057:/bin/bash
>
> But I want it to give:
> 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

Trick question? Have you tried this?

* drop the "for user in"
* lose the backticks around the cat command
* remove the "do" and "done".

which results in this:

$ cat 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

Looks to me like the desired output. Problem solved.

Rakesh Sharma

unread,
Apr 28, 2016, 1:25:17 AM4/28/16
to
On Thursday, 28 April 2016 02:28:31 UTC+5:30, cnu...@yahoo.com wrote:

cut -d: -f5 name_list

Andre Majorel

unread,
Apr 28, 2016, 4:46:56 AM4/28/16
to
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.

cnu...@yahoo.com

unread,
Apr 28, 2016, 9:44:15 AM4/28/16
to
That did it. Thanks Marek!

applemcg

unread,
May 10, 2016, 2:35:01 PM5/10/16
to
any request to "process lines from a file" should always consider using awk.

Not having said what "processing" you want to do, here's an example:

$ awk -F: '{ print $5 }' linesFromFile.txt
Joe Smith
Bill Brown
Don Quioxte

in awk, the -F flag sets the FIELD separator, which defaults to any blank space.

awk's model is to read a line at a time, match a leading expression and perform the associated action. you may have many PATTERN { ACTION } pairs in an awk script. for example if you only want non-blank lines:

$ awk -F: 'NF { print $5 }' linesFromFile.txt

the leading pattern, NF is the awk builtin for Number of Fields, and since any nonblank line has at least one field, it only prints output for the non-blank lines..

good luck.
0 new messages