[NTLUG:Discuss] Extracting a column out of input

Leroy Tennison leroy.tennison at verizon.net
Sun Jan 24 21:11:36 CST 2016


The context is I want to extract only the package name out of Ubuntu's 
'dpkg -' where the first column is the install status, the second the 
package name and the rest a description.  More generally though I want 
to know how to extract any column of data out of a multi-column list.  
I've searched the web and found one way to do it but would like to have 
the flexibility of others without having to resort to awk, perl, etc.  
My sense is that there is enough bash experience in this group that I 
can get an answer here a lot quicker than trying to find exactly what I 
want on the web.  What I have so far is:

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

I was trying

for i in `dpkg -l`; do read col1 col2 col3; echo col2; done

but read is expecting stdin.  Adding 'xargs -l1' in front produced "No 
such file or directory".

Then I tried

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

to get a syntax error and

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

to get "Ambiguous redirect"

What am I doing wrong and what should I do?




More information about the Discuss mailing list