[NTLUG:Discuss] Extracting a column out of input

Robert Citek robert.citek at gmail.com
Mon Jan 25 19:42:34 CST 2016


On Sun, Jan 24, 2016 at 7:11 PM, Leroy Tennison
<leroy.tennison at verizon.net> wrote:
> dpkg -l | while read col1 col2 col3; do echo $col2; done

That works.

> I was trying
>
> for i in `dpkg -l`; do read col1 col2 col3; echo col2; done

The "for" construct doesn't really understand lines nor splitting the
lines as "while" does.

> while read col1 col2 col3; do echo $col2; done < ls -l
...
> while read col1 col2 col3; do echo $col2; done < `ls -l`

Redirecting (i.e. < or >) is used for files.  To redirect the
input/output to/from a process, use a pipe.  For example:

$ ls -l  | while read col1 col2 col3; do echo $col2; done

> What am I doing wrong and what should I do?

Your first command looks good to me.  I'd go with that.

Regards,
- Robert



More information about the Discuss mailing list