franklin
franklin

Reputation: 1819

indentation with linux command line tools

i'm currently writing a linux script that has the following code:

echo "\nCompiled on:\n `gcc --version`\n"

the output is formatted as such:

Compiled on:
gcc (GCC) 3.4.4 etc...
This is free software...

however i want the command to be formatted like this:

Compiled on:
    gcc(GCC) 3.4.4 etc...
    This is free software...

how can i format the text so that each LINE outputted from the gcc --version command has its own tab?

Upvotes: 2

Views: 928

Answers (1)

cyang
cyang

Reputation: 5694

How about:

echo "\nCompiled on:\n $(gcc --version | sed 's/^/\t/g' )\n"

The output of gcc --version is piped to sed, which adds a tab at the beginning of the line.

Alternatively, you could use awk (could be shorter if you don't need the extra newlines):

gcc --version | awk 'BEGIN { print "\nCompiled on:" } { print "\t" $0 } END { printf RS }'

Upvotes: 2

Related Questions