Tom Tom - 1 year ago 65
Bash Question

What's the parameter call for STDIN when you pipe things into echo or print?

I have some text that I've created using a sting of pipes, and I'd like to further manipulate that using echo instead of awk.

Say that I have:

a chain | of | commands | producing |stream | of text


line 1 text
line 2 text
line 3 text

But if I wanted to use echo to do something like this:

a chain | of | commands | producing |stream | of text | echo ${STDIN%text}

to cut off all text after where it says line, how would I reference that to get this expected result:

line 1
line 2
line 3

Answer Source

A tool like awk is best for your scenario :

a chain | of | commands | awk '{sub(/[[:blank:]]*text$/,"")}1'

sub by default acts on the whole record $0 and will retain the number of spaces/tabs in the text retrieved.

Need more on awk? check [ this ].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download