How do I create files using the lines in a .txt file? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionI need to read every line, and to create an empty file after reading each and every lineSplit a file by line and have control over resulting files extensionHow to know if a file has been written completely?Using 'find' to create text file, and later deleting files listedextracting lines of text from a long fileDynamically extract comments from files using catUsing touch command and wildcard how to solve?Creating text file with content within a mkdir listHow to list all files in named directory, file type included?echo line with var that contains few linesReplace lines in multiple files matching a pattern with lines from another file in order
Maximum summed powersets with non-adjacent items
Do wooden building fires get hotter than 600°C?
Can a new player join a group only when a new campaign starts?
What do you call the main part of a joke?
Compare a given version number in the form major.minor.build.patch and see if one is less than the other
2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?
Around usage results
Trademark violation for app?
Dating a Former Employee
What is the longest distance a player character can jump in one leap?
Do I really need to have a message in a novel to appeal to readers?
Delete nth line from bottom
Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?
How can I use the Python library networkx from Mathematica?
What does "lightly crushed" mean for cardamon pods?
Generate an RGB colour grid
Why do we bend a book to keep it straight?
An adverb for when you're not exaggerating
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
How to react to hostile behavior from a senior developer?
How do I stop a creek from eroding my steep embankment?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
How do pianists reach extremely loud dynamics?
Wu formula for manifolds with boundary
How do I create files using the lines in a .txt file?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionI need to read every line, and to create an empty file after reading each and every lineSplit a file by line and have control over resulting files extensionHow to know if a file has been written completely?Using 'find' to create text file, and later deleting files listedextracting lines of text from a long fileDynamically extract comments from files using catUsing touch command and wildcard how to solve?Creating text file with content within a mkdir listHow to list all files in named directory, file type included?echo line with var that contains few linesReplace lines in multiple files matching a pattern with lines from another file in order
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
shell
add a comment |
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.
shell
add a comment |
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.
shell
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.
shell
shell
edited Apr 13 at 15:21
Rui F Ribeiro
42.1k1484142
42.1k1484142
asked Oct 2 '15 at 3:32
I'm a noobI'm a noob
497
497
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only onetouchcommand per many hundreds of filenames rather than one touch per filename.
– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
add a comment |
You can use an input redirection in a while loop with the read command
while read line; do
touch "$line.txt"
done < fileNames.txt
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with-. That also means running onetouchcommand per file.
– Stéphane Chazelas
Jun 28 '17 at 8:31
add a comment |
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.
@Emmanuel Dieguez's answer should work as well (without usingawk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.
– rubynorails
Oct 2 '15 at 3:46
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f233408%2fhow-do-i-create-files-using-the-lines-in-a-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only onetouchcommand per many hundreds of filenames rather than one touch per filename.
– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
add a comment |
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.
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only onetouchcommand per many hundreds of filenames rather than one touch per filename.
– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
add a comment |
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.
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.
edited Jun 28 '17 at 8:28
Stéphane Chazelas
315k57597955
315k57597955
answered Oct 2 '15 at 3:39
cascas
39.6k456103
39.6k456103
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only onetouchcommand per many hundreds of filenames rather than one touch per filename.
– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
add a comment |
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only onetouchcommand per many hundreds of filenames rather than one touch per filename.
– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
Thanks. This answer is probably the easiest to understand. sed and xargs were the two things I was missing. I had the right conceptual stuff, just not the commands to do it. xargs seems like a really powerful command.
– I'm a noob
Oct 2 '15 at 3:55
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
yes, xargs is extremely useful.
– cas
Oct 2 '15 at 3:57
1
1
using xargs is also more efficient than a for or while etc loop around touch - it executes only one
touch command per many hundreds of filenames rather than one touch per filename.– cas
Oct 2 '15 at 4:07
using xargs is also more efficient than a for or while etc loop around touch - it executes only one
touch command per many hundreds of filenames rather than one touch per filename.– cas
Oct 2 '15 at 4:07
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
Wow that's waaaaay more efficient. Thanks for the help.
– I'm a noob
Oct 2 '15 at 4:49
add a comment |
You can use an input redirection in a while loop with the read command
while read line; do
touch "$line.txt"
done < fileNames.txt
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with-. That also means running onetouchcommand per file.
– Stéphane Chazelas
Jun 28 '17 at 8:31
add a comment |
You can use an input redirection in a while loop with the read command
while read line; do
touch "$line.txt"
done < fileNames.txt
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with-. That also means running onetouchcommand per file.
– Stéphane Chazelas
Jun 28 '17 at 8:31
add a comment |
You can use an input redirection in a while loop with the read command
while read line; do
touch "$line.txt"
done < fileNames.txt
You can use an input redirection in a while loop with the read command
while read line; do
touch "$line.txt"
done < fileNames.txt
answered Oct 2 '15 at 3:43
Emmanuel DiéguezEmmanuel Diéguez
312
312
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with-. That also means running onetouchcommand per file.
– Stéphane Chazelas
Jun 28 '17 at 8:31
add a comment |
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with-. That also means running onetouchcommand per file.
– Stéphane Chazelas
Jun 28 '17 at 8:31
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
This is much cleaner and more portable than the accepted answer.
– Peschke
Oct 3 '15 at 10:00
1
1
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with
-. That also means running one touch command per file.– Stéphane Chazelas
Jun 28 '17 at 8:31
@Peschke, that is more portable (assuming Bourne-like shells), but not really cleaner. That won't work properly if the lines contain leading or trailing space or tabs, or start with
-. That also means running one touch command per file.– Stéphane Chazelas
Jun 28 '17 at 8:31
add a comment |
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.
@Emmanuel Dieguez's answer should work as well (without usingawk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.
– rubynorails
Oct 2 '15 at 3:46
add a comment |
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.
@Emmanuel Dieguez's answer should work as well (without usingawk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.
– rubynorails
Oct 2 '15 at 3:46
add a comment |
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 '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.
edited Oct 2 '15 at 3:51
answered Oct 2 '15 at 3:40
rubynorailsrubynorails
1,292516
1,292516
@Emmanuel Dieguez's answer should work as well (without usingawk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.
– rubynorails
Oct 2 '15 at 3:46
add a comment |
@Emmanuel Dieguez's answer should work as well (without usingawk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.
– rubynorails
Oct 2 '15 at 3:46
@Emmanuel Dieguez's answer should work as well (without using
awk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.– rubynorails
Oct 2 '15 at 3:46
@Emmanuel Dieguez's answer should work as well (without using
awk), but on extremely large files, removing empty lines before cycling through each one in a while loop may speed things up a bit.– rubynorails
Oct 2 '15 at 3:46
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f233408%2fhow-do-i-create-files-using-the-lines-in-a-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown