Unwanted line break using echo and cat

后端 未结 3 554
无人共我
无人共我 2021-01-24 03:33

I\'m trying to add a line at the beginning of a file, using

echo \'time/F:x1:x2\' | cat - file.txt>newfile.txt

But this produces line breaks

相关标签:
3条回答
  • 2021-01-24 04:11

    Use -n to disable the trailing newline:

    echo -n 'time/F:x1:x2' | cat - file.txt > newfile.txt
    

    There are other ways, too:

    sed '1s|^|time/F:x1:x2|' file.txt > newfile.txt
    
    0 讨论(0)
  • 2021-01-24 04:12

    Actually you don't even need the echo and pipe if you're using bash. Just use a herestring:

    <<< 'time/F:x1:x2' cat - file.txt > newfile.txt
    
    0 讨论(0)
  • 2021-01-24 04:23

    How about

    { echo 'time/F:x1:x2'; cat file.txt; } >newfile.txt
    

    or

    sed '1i\
    time/F:x1:x2' file.txt > newfile.txt
    
    0 讨论(0)
提交回复
热议问题