Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Converting space separated output to one per line

leave a comment »

Here is the problem I was faced with. I wanted to check if I was part of a particular group and I did the shell groups command to find out.

Unbeknownst to me I was part of a tone of groups that included some complicated long names with backslashes (\) in them.

I thought it would be quick to grep for the particular group name if all the output was on separate lines instead of space separated on one line.

First I thought I can use cut command to do that but it turns out cut is not idea for this. It is more suitable if you have multiple lines of data and wanted to get the same column out of each of these. Something like output from ps.

Finally I achieved it really easily with with tr command. Here is how the solution looks like:


groups | tr ' ' '\n' | grep group_i_was_looking_for

Advertisements

Written by imsaar

January 13, 2012 at 1:04 pm

Posted in general

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: