How to concatenate stdin and a string?

前端 未结 9 1451
死守一世寂寞
死守一世寂寞 2021-01-30 01:49

How to I concatenate stdin to a string, like this?

echo \"input\" | COMMAND \"string\"

and get

inputstring
9条回答
  •  生来不讨喜
    2021-01-30 02:35

    There are some ways of accomplish this, i personally think the best is:

    echo input | while read line; do echo $line string; done
    

    Another can be by substituting "$" (end of line character) with "string" in a sed command:

    echo input | sed "s/$/ string/g"
    

    Why i prefer the former? Because it concatenates a string to stdin instantly, for example with the following command:

    (echo input_one ;sleep 5; echo input_two ) | while read line; do echo $line string; done
    

    you get immediatly the first output:

    input_one string
    

    and then after 5 seconds you get the other echo:

    input_two string
    

    On the other hand using "sed" first it performs all the content of the parenthesis and then it gives it to "sed", so the command

    (echo input_one ;sleep 5; echo input_two ) | sed "s/$/ string/g"
    

    will output both the lines

    input_one string
    input_two string
    

    after 5 seconds.

    This can be very useful in cases you are performing calls to functions which takes a long time to complete and want to be continuously updated about the output of the function.

提交回复
热议问题