Reputation: 827
Say I have a directory containing 5 files :
file1.md
another file.md
and yet another one.md
some other file.md
And the last one.md
Files may have spaces in their name.
Now suppose I have a file containing an ordered list of the filenames above :
And the last one.md
another file.md
and yet another one.md
file1.md
some other file.md
What Bash script could I use in order to concatenate all six files into a new one, following the order defined in the ordered list ? I'm using Bash on Windows. The context is that I want to be able to reorder small parts of a larger text, such as short stories or chapters in a book.
Upvotes: 1
Views: 645
Reputation: 1229
You can use while read
.
rm output.txt;cat input.txt | while read row; do cat "$row" >> output.txt; done
Alternatively:
while read row; do cat "$row"; done < input.txt > output.txt
The command reads rows from the input.txt
and appends the contents of the files to the output.txt
. Double quotes are good for spaces in file names.
My attempt:
Creating data (optional):
while read row; do echo "$row" "mm" > "$row"; done
Shift + Ins
Ctrl + c
$ cat file1.md
file1.md mm
$ ls -1
'And the last one.md'
'and yet another one.md'
'another file.md'
file1.md
input.txt
output.txt
'some other file.md'
Output:
$ cat output.txt
And the last one.md mm
another file.md mm
and yet another one.md mm
file1.md mm
some other file.md mm
Change order of names:
$ cat input.txt
another file.md
and yet another one.md
some other file.md
And the last one.md
file1.md
$ rm output.txt;cat input.txt | while read row; do cat "$row" >> output.txt; done
$ cat output.txt
another file.md mm
and yet another one.md mm
some other file.md mm
And the last one.md mm
file1.md mm
Upvotes: 0
Reputation: 140960
The following should be just enough:
xargs -d '\n' cat < file_containing_an_ordered_list_of_the_filenames.txt
Upvotes: 3