How to make cat start a new line

前端 未结 6 558
情话喂你
情话喂你 2021-01-18 02:24

I have four files:

one_file.txt

abc | def

two_file.txt

ghi | jkl

three_file.txt

m         


        
相关标签:
6条回答
  • 2021-01-18 02:39
    find . -name "*file.txt" | xargs cat > full_set.txt
    
    0 讨论(0)
  • 2021-01-18 02:47

    You can loop over each file and do a check to see if the last line ends in a new line, outputting one if it doesn't.

    for file in *file.txt; do
        cat "$file"
        [[ $(tail -c 1 "$file") == "" ]] || echo
    done > full_set.txt
    
    0 讨论(0)
  • 2021-01-18 02:47

    You can use one line for loop for this. The following line:

    for f in *_file.txt; do (cat "${f}") >> full_set.txt; done
    

    Yields the desired output:

    $ cat full_set.txt 
    abc | def
    mno | pqr
    ghi | jkl
    

    Also, possible duplicate.

    0 讨论(0)
  • 2021-01-18 02:47

    this works for me:

    for file in $(ls *file.txt) ; do cat $file ; echo ; done > full_set.txt
    

    I hope this will help you.

    0 讨论(0)
  • 2021-01-18 02:54

    Many tools will add newlines if they are missing. Try e.g.

    sed '' *file.txt >full_set.txt
    

    but this depends on your sed version. Others to try include Awk, grep -ho '.*' file*.txt and etc.

    0 讨论(0)
  • 2021-01-18 03:01

    Try:

    awk 1 *file.txt > full_set.txt
    

    This is less efficient than a bare cat but will add an extra \n if missing at the end of each file

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