Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a piped command say

command1 | command2 | command3

or lets say something like

ps | grep "something" 

Now to the output of the command I would like to add to each coloumn some label or data to the top using shell script.

EDIT
In short this is what i want

InsertedLabel1   Inslabel2        Inslabel3
Data1frompipe     Data1frompipe     Data1frompipe
Data2frompipe    Data2frompipe     Data2frompipe

What is an easy way to acheive this?

share|improve this question

2 Answers

up vote 1 down vote accepted

If you want the headers to line up with the columns, you can use the aptly named column utility (a BSD extension, but it also comes with most Linux distros). To reformat existing text into aligned columns, use the -t option.

You can insert the column headers using a compound statement:

command1 | command2 | { echo Header1 Header2 Header3; command3 } | column -t

or:

{ echo Header1 Header2 Header3; command1 | command2 | command3 } | column -t

(whichever you find more readable.)

Note that the headers may not have spaces in them, and nor may the data-elements. If your data is not white-space separated, you can specify a different delimiter with the -s option; remember to use the same delimiter for your headers.

column left-justifies all columns, so numeric columns don't look as nice as you might want them to.

share|improve this answer

You could use blocks in shells to insert another command and use it to insert lines before or after the output of the other command e.g. echo before grep:

ps | { echo "header"; grep "something"; }

To make it easier for you in a script you could use this form:

ps | {
    echo "header"
    grep "something"
    # possibly other echos here.
}

In awk you could use BEGIN:

ps | awk 'BEGIN { print "header"; } /something/;'

And/or END to add tailing lines:

ps | awk 'BEGIN { print "header"; } /something/; END { print "------"; }'

Of course if you have more than two commands you could just use the form on the last

command | command | { echo "header"; grep "something"; }

Or

command | command | awk 'BEGIN { print "header"; } /something/;'
share|improve this answer
waht if the returned output had multiple coloumns,and i want to add to the top of each coloumn different labels? – Stormvirux Aug 9 at 6:20
just see the updated question – Stormvirux Aug 9 at 6:24
@Stormvirux That would be specific to the output but basically wouldn't formatting the string ("header") be able to achieve that? e.g. replace "header" with "InsertedLabel1 Inslabel2 Inslabel3" – konsolebox Aug 9 at 6:25
What about the spacing between the labels?? If the data length increases will labels alighb accordinly?No right?? – Stormvirux Aug 9 at 6:27
1  
@Stormvirux Well yes and in that case that's no longer just about adding one header row to your output as per your original question and is more complex. You'll need to use awk to read the whole set of lines and calculate the columns and the maximum length for each. On the END block you would have to print it. – konsolebox Aug 9 at 6:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.