Jacek
Jacek

Reputation: 765

How to separate contents of a file into columns based on keyword using Bash

I have a file.txt that has contents in the following format:

co001 done
co002 done
co003 not run
co004 running

I want to read the file.txt and sort the lines into columns based on whether they contain "done", "not run" or "running" and display the following on screen

co001 done     co003 not run     co004 running
co002 done

So far I know I can use grep to extract the "done" or "not run" lines, but I don't know how to sort the grep results into columns. I figured that the "column" command will help somehow, but cannot think of how to go about doing it.

This should be an one-liner thing right?

Upvotes: 1

Views: 58

Answers (1)

tshiono
tshiono

Reputation: 22022

How about combining the paste command with the process substitution provided by bash:

paste <(grep "done" file.txt) <(grep "not run" file.txt) <(grep "running" file.txt)

Upvotes: 1

Related Questions