Linux: Merging multiple files, each on a new line

后端 未结 5 377
予麋鹿
予麋鹿 2021-01-30 14:32

I am using cat *.txt to merge multiple txt files into one, but I need each file to be on a separate line.

What is the best way to merge files with each

相关标签:
5条回答
  • 2021-01-30 14:47
    for file in *.txt
    do
      cat "$file"
      echo
    done > newfile
    
    0 讨论(0)
  • 2021-01-30 14:51

    just use awk

    awk 'FNR==1{print ""}1' *.txt
    
    0 讨论(0)
  • 2021-01-30 14:59

    You can iterate through each file with a for loop:

    for filename in *.txt; do
        # each time through the loop, ${filename} will hold the name
        # of the next *.txt file.  You can then arbitrarily process
        # each file
        cat "${filename}"
        echo
    
    # You can add redirection after the done (which ends the
    # for loop).  Any output within the for loop will be sent to
    # the redirection specified here
    done > output_file
    
    0 讨论(0)
  • 2021-01-30 15:03

    If you have a paste that supports it,

    paste --delimiter=\\n --serial *.txt
    

    does a really great job

    0 讨论(0)
  • 2021-01-30 15:07

    I'm assuming you want a line break between files.

    for file in *.txt
    do
       cat "$file" >> result
       echo >> result
    done
    
    0 讨论(0)
提交回复
热议问题