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
I ended up with this:
sed '/^$/d;/^#/d;s/^/cat "/;s/$/";/' files | sh > output
Steps:
sed removes blank lines & commented out lines from files.
sed then wraps each line in cat "
and ";
.
Script is then piped to sh with and into output.
Or in a simple command
cat $(grep -v '^#' files) > output
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
#!/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:
cat
, only gets executed once.{
while read file
do
#process comments here with continue
cat "$file"
done
} < tmp > newfile
How about cat $(cat listoffiles) | grep -v "^#"
?