help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] set and IFS


From: Greg Wooledge
Subject: Re: [Help-bash] set and IFS
Date: Tue, 20 Nov 2018 08:19:12 -0500
User-agent: NeoMutt/20170113 (1.7.2)

On Tue, Nov 20, 2018 at 11:51:07AM +0100, Gérard ROBIN wrote:
> g:gg:ggg:1:bin
> q:qq:qqq:2:bin
> f:ff:fff:3:bin
> k:*:kkk:4:bin

It seems you are really working with /etc/passwd as your input.

> the output of ./nomutilsuid.sh
> 
> g 1
> q 2
> f 3
> k danbin.sh

So the desired output is the username, a space, and the primary GID?
Here's how it should be done in pure bash:

while IFS=: read -r user _ _ gid _; do
  printf '%s %s\n' "$user" "$gid"
done < /etc/passwd

It could also be done in awk:

awk -F: '{print $1, $4}' /etc/passwd

You will note that the awk solution is way shorter, and runs faster.
Awk is widely considered the most elegant tool for certain jobs of
this kind.

What really matters is the *final* goal, which you haven't revealed yet.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]