How to cat multiple files from a list of files in Bash?

后端 未结 6 785
故里飘歌
故里飘歌 2020-12-16 18:08

I have a text file that holds a list of files. I want to cat their contents together. What is the best way to do this? I was doing something like this but it

相关标签:
6条回答
  • 2020-12-16 18:41

    I ended up with this:

    sed '/^$/d;/^#/d;s/^/cat "/;s/$/";/' files | sh > output
    

    Steps:

    1. sed removes blank lines & commented out lines from files.

    2. sed then wraps each line in cat " and ";.

    3. Script is then piped to sh with and into output.

    0 讨论(0)
  • 2020-12-16 18:43

    Or in a simple command

    cat $(grep -v '^#' files) > output
    
    0 讨论(0)
  • 2020-12-16 18:45

    xargs

    The advantage of xargs over $(cat) is that cat expands to a huge list of arguments which could fail if you have a lot of files in the list:

    printf 'a\nb\n#c\n' > files
    printf '12\n3\n' > a
    printf '4\n56\n' > b
    printf  '8\n9\n' > c
    # Optional grep to remove lines starting with #
    # as requested by the OP.
    grep -v '^#' files | xargs cat
    

    Output:

    12
    3
    4
    56
    

    Related: How to pipe list of files returned by find command to cat to view all the files

    0 讨论(0)
  • 2020-12-16 18:48
    #!/bin/bash
    
    files=()
    while read; do
        case "$REPLY" in
            \#*|'') continue;;
            *) files+=( "$REPLY" );;
        esac
    done < input
    cat "${files[@]}"
    

    What's better about this approach is that:

    1. The only external command, cat, only gets executed once.
    2. It's pretty careful to maintain significant whitespace for any given line/filename.
    0 讨论(0)
  • 2020-12-16 18:54
    {
      while read file
      do
        #process comments here with continue
        cat "$file"
      done
    } < tmp > newfile
    
    0 讨论(0)
  • 2020-12-16 19:01

    How about cat $(cat listoffiles) | grep -v "^#"?

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