For example I have a file called fileNames.txt. cat >fileNames returns
abet
able
abacus
How do I use fileNames to create abet, able, and abacus as .txt files? I tried using pipes with cat and touch.
There are numerous ways to do that, including the following (assuming GNU xargs):
sed -e '/^[[:space:]]*$/d' -e 's/$/.txt/' fileNames.txt | xargs -rd '\n' touch --
This assumes that there is only one filename per line in fileNames.txt. The sed script deletes blank lines (thanks to @rubynorails) and adds '.txt' to the end of each filename before piping them to xargs touch.
By using the -d '\n' option with xargs (a GNU extension, which tells xargs to use only newline or '\n' as the delimiter rather than any whitespace, and disables quote processing) this even copes with filenames that have spaces and other potentially-problematic characters (except for \n or newline itself). e.g. if a line contained 'abet able', without -d '\n', two files (abet and able.txt) would be created. With it, only one file abet able.txt will be created.
touch command per many hundreds of filenames rather than one touch per filename.
You can use an input redirection in a while loop with the read command
while read line; do
touch "${line}.txt"
done < fileNames.txt
-. That also means running one touch command per file.
awk 'NF>0' fileNames.txt | while read line; do touch "${line}.txt"; done
The awk command just gets rid of the empty lines.
To the best of my knowledge, these lines are also literal, so whatever is on the line is getting written as a file name.
awk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.