Bash: cat multiple files from the file list

I have a text file containing a list of files. I want to cat their contents together. What is the best way to do this? I did something like this, but it seems too complicated:

 let count=0 while read -r LINE do [[ "$line" =~ ^#.*$ ]] && continue if [ $count -ne 0 ] ; then file="$LINE" while read PLINE do echo $PLINE | cat - myfilejs > /tmp/out && mv /tmp/out myfile.js done < $file fi let count++ done < tmp 

I skipped the commented lines and ran into problems. There should be a better way to do this without two cycles. Thanks!

+6
source share
6 answers

Or in a simple team

 cat $(grep -v '^#' files) > output 
+9
source
 #!/bin/bash files=() while read; do case "$REPLY" in \#*|'') continue;; *) files+=( "$REPLY" );; esac done < input cat "${files[@]}" 

What is better in this approach is:

  • The only external cat that runs only once.
  • Very carefully maintain significant spaces for any line / file name.
+5
source
 { while read file do #process comments here with continue cat "$file" done } < tmp > newfile 
+3
source

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

+1
source

I ended up with this:

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

Steps:

  • sed deletes empty lines and comments lines from files.

  • sed ends each line in cat " and "; .

  • Script is then passed on the bus and output.

0
source

xargs

 printf 'a\nb\n' > files printf '12\n3\n' > a printf '4\n56\n' > b xargs cat < files 

Conclusion:

 12 3 4 56 
0
source

Source: https://habr.com/ru/post/921153/


All Articles