Extracting words from .txt and creating one master .txt
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?
linux shell-script
add a comment |Â
up vote
1
down vote
favorite
I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?
linux shell-script
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?
linux shell-script
I have multiple files .txt which contain names of people sorted alphabetically so for example in main directory I have directory a which contains one a.txt full with names which start with "a" like "Anna" "Andrew" etc. The same thing repeats in main directory I have directory b containing b.txt full with names etc. up until x,y,z. How can I extract the names and create a master.txt containing all people's names?
linux shell-script
asked Nov 19 '17 at 1:52
Alex
1104
1104
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory
, and there are no other files in this directory, then the most succinct command would be the following:
cat /path/to/directory/*/*.txt > master.txt
This will create a file called master.txt
in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.
If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:
cat /path/to/directory/[a-z]/[a-z].txt > master.txt
This will only match the following files in the source directory:
a/a.txt
a/b.txt
a/c.txt
.
.
.
z/x.txt
z/y.txt
z/z.txt
If there are other files in the source directory, or if you have files of the similar to a/z.txt
where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:
for letter in a..z; do
cat "/path/to/directory/$letter/$letter.txt";
done >> master.txt
This will match exactly the files you've specified in your question and no other files.
add a comment |Â
up vote
0
down vote
Replace main_dir
with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh
#!/bin/bash
for i in $( cd main_dir && ls ); do
cat "main_dir/$i/$i.txt" >> "master.txt"
done
There's no reason to parse the output ofls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output ofls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
â igal
Nov 19 '17 at 4:01
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory
, and there are no other files in this directory, then the most succinct command would be the following:
cat /path/to/directory/*/*.txt > master.txt
This will create a file called master.txt
in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.
If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:
cat /path/to/directory/[a-z]/[a-z].txt > master.txt
This will only match the following files in the source directory:
a/a.txt
a/b.txt
a/c.txt
.
.
.
z/x.txt
z/y.txt
z/z.txt
If there are other files in the source directory, or if you have files of the similar to a/z.txt
where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:
for letter in a..z; do
cat "/path/to/directory/$letter/$letter.txt";
done >> master.txt
This will match exactly the files you've specified in your question and no other files.
add a comment |Â
up vote
2
down vote
You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory
, and there are no other files in this directory, then the most succinct command would be the following:
cat /path/to/directory/*/*.txt > master.txt
This will create a file called master.txt
in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.
If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:
cat /path/to/directory/[a-z]/[a-z].txt > master.txt
This will only match the following files in the source directory:
a/a.txt
a/b.txt
a/c.txt
.
.
.
z/x.txt
z/y.txt
z/z.txt
If there are other files in the source directory, or if you have files of the similar to a/z.txt
where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:
for letter in a..z; do
cat "/path/to/directory/$letter/$letter.txt";
done >> master.txt
This will match exactly the files you've specified in your question and no other files.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory
, and there are no other files in this directory, then the most succinct command would be the following:
cat /path/to/directory/*/*.txt > master.txt
This will create a file called master.txt
in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.
If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:
cat /path/to/directory/[a-z]/[a-z].txt > master.txt
This will only match the following files in the source directory:
a/a.txt
a/b.txt
a/c.txt
.
.
.
z/x.txt
z/y.txt
z/z.txt
If there are other files in the source directory, or if you have files of the similar to a/z.txt
where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:
for letter in a..z; do
cat "/path/to/directory/$letter/$letter.txt";
done >> master.txt
This will match exactly the files you've specified in your question and no other files.
You can accomplish this by using the cat command and filename expansion. If all of these files are in the source directory /path/to/directory
, and there are no other files in this directory, then the most succinct command would be the following:
cat /path/to/directory/*/*.txt > master.txt
This will create a file called master.txt
in your current directory which contains the concatenated contents of all of the files in the source directory. NOTE: This will include any files in any of the subdirectories of the source directory.
If there are other files in the directory (or if you just want to be a little bit more precise) then you can use this command instead:
cat /path/to/directory/[a-z]/[a-z].txt > master.txt
This will only match the following files in the source directory:
a/a.txt
a/b.txt
a/c.txt
.
.
.
z/x.txt
z/y.txt
z/z.txt
If there are other files in the source directory, or if you have files of the similar to a/z.txt
where the subdirectory name doesn't match the base-name of the file, and if you want to exclude those files, then you would have to use a more precise command to narrow down the list of matched files. In that case you could use brace-expansion and a for-loop:
for letter in a..z; do
cat "/path/to/directory/$letter/$letter.txt";
done >> master.txt
This will match exactly the files you've specified in your question and no other files.
edited Nov 19 '17 at 3:26
answered Nov 19 '17 at 2:18
igal
4,830930
4,830930
add a comment |Â
add a comment |Â
up vote
0
down vote
Replace main_dir
with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh
#!/bin/bash
for i in $( cd main_dir && ls ); do
cat "main_dir/$i/$i.txt" >> "master.txt"
done
There's no reason to parse the output ofls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output ofls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
â igal
Nov 19 '17 at 4:01
add a comment |Â
up vote
0
down vote
Replace main_dir
with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh
#!/bin/bash
for i in $( cd main_dir && ls ); do
cat "main_dir/$i/$i.txt" >> "master.txt"
done
There's no reason to parse the output ofls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output ofls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
â igal
Nov 19 '17 at 4:01
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Replace main_dir
with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh
#!/bin/bash
for i in $( cd main_dir && ls ); do
cat "main_dir/$i/$i.txt" >> "master.txt"
done
Replace main_dir
with the path to your main directory, save this in a script and run from the terminal as sh ./script-name.sh
#!/bin/bash
for i in $( cd main_dir && ls ); do
cat "main_dir/$i/$i.txt" >> "master.txt"
done
answered Nov 19 '17 at 2:23
shivench
215
215
There's no reason to parse the output ofls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output ofls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
â igal
Nov 19 '17 at 4:01
add a comment |Â
There's no reason to parse the output ofls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output ofls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.
â igal
Nov 19 '17 at 4:01
There's no reason to parse the output of
ls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.â igal
Nov 19 '17 at 4:01
There's no reason to parse the output of
ls
in this situation, since globbing/filename-expansion will work just as well. Also, parsing the output of ls
is considered bad practice in general. See Why you shouldn't parse the output of ls for more on that topic.â igal
Nov 19 '17 at 4:01
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405522%2fextracting-words-from-txt-and-creating-one-master-txt%23new-answer', 'question_page');
);
Post as a guest
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
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
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