How to concatenate stdin and a string?

前端 未结 9 1452
死守一世寂寞
死守一世寂寞 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:23

    I know this is a few years late, but you can accomplish this with the xargs -J option:

    echo "input" | xargs -J "%" echo "%" "string"
    

    And since it is xargs, you can do this on multiple lines of a file at once. If the file 'names' has three lines, like:

    Adam
    Bob
    Charlie
    

    You could do:

    cat names | xargs -n 1 -J "%" echo "I like" "%" "because he is nice"
    
    0 讨论(0)
  • 2021-01-30 02:25

    I'm often using pipes, so this tends to be an easy way to prefix and suffix stdin:

    echo -n "my standard in" | cat <(echo -n "prefix... ") - <(echo " ...suffix")
    prefix... my standard in ...suffix
    
    0 讨论(0)
  • 2021-01-30 02:25

    You can do it with sed:

    seq 5 | sed '$a\6'
    seq 5 | sed '$ s/.*/\0 6/'
    

    In your example:

    echo input | sed 's/.*/\0string/'
    
    0 讨论(0)
  • 2021-01-30 02:26

    cat will be my choice: ls | cat - <(echo new line)

    0 讨论(0)
  • 2021-01-30 02:34

    The command you posted would take the string "input" use it as COMMAND's stdin stream, which would not produce the results you are looking for unless COMMAND first printed out the contents of its stdin and then printed out its command line arguments.

    It seems like what you want to do is more close to command substitution.

    http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution

    With command substitution you can have a commandline like this:

    echo input `COMMAND "string"`
    

    This will first evaluate COMMAND with "string" as input, and then expand the results of that commands execution onto a line, replacing what's between the ‘`’ characters.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题