[NTLUG:Discuss] Extracting a column out of input

Leroy Tennison leroy.tennison at verizon.net
Mon Jan 25 21:55:31 CST 2016


Thank you for two points: use tr to squeeze the white space for cut and 
how to specify a space as a cut delimiter.  I appreciate it.

On 01/25/2016 07:28 PM, Stuart Johnston wrote:
> The traditional command for selecting columns of data is 'cut'. The 
> problem though is that cut requires a single character separator 
> rather than the multiple whitespace dpkg gives you. This is why awk or 
> perl would be more flexible.
>
> One solution is to "collapse" the whitespace with 'tr':
>
> dpkg -l | tr -s ' ' | cut -d' ' -f2
>
>
>
> Quoting Leroy Tennison <leroy.tennison at verizon.net>:
>
>> 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?
>>
>>
>> _______________________________________________
>> http://www.ntlug.org/mailman/listinfo/discuss
>>
>
>
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>




More information about the Discuss mailing list