Why in shell script output file get written as single line?

前端 未结 2 789
慢半拍i
慢半拍i 2021-01-23 12:24

Input file: ankit.txt with header date and trailer count

H2014-12-02
12ASDF23 FGHJ HJKL
123ASD23 FGHJ HJKL
123ASD23 FGHJ HJKL
123ASD23 FGHJ HJKL
T000004
<         


        
相关标签:
2条回答
  • 2021-01-23 12:41

    Looks like you've got Windows line endings. You've got two options. Either pre-process your input file to give it UNIX endings:

    dos2unix ankit.txt
    

    or else do that as part of your pipe:

    dos2unix < ankit.txt | head -n -1 | tail -n +2 > output
    

    Note that dos2unix has two modes. In the first case, it'll take the input file and rewrite it with UNIX line endings (which is ideal for pre-processing). In the second, it'll read from stdin and write the new output to stdout (which is ideal for pipes).

    0 讨论(0)
  • 2021-01-23 13:06

    Using

    echo `command`
    

    is not only wasteful, it will also perform whitespace splitting and glob expansion on the output from command before passing it to echo. Usually you simply want

    command
    

    or if you insist on doing a process substitution, and expect its output to be preserved, put it in double quotes:

    echo "$(command)"
    

    (Notice also the switch to the modern, recommended syntax for process substitution.)

    But really, why would you want that? If you really do, would it not be better still to have

    echo "$(echo "$(echo "$(echo "$(echo ...
    
    0 讨论(0)
提交回复
热议问题