how to copy data from multiple txt to a single txt file according to the order [closed]
Clash Royale CLAN TAG#URR8PPP
I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
I have also used the below command so i can control the copy format
for tablename in 'cat $current_path/table.txt(have defined the order)
do
for data in 'cat path/$tablename.txt
do
echo "$data" >> final/output.txt
done
done
here it is doing its work but generating multiple copies of the single txt as for loop is working .
linux shell-script
closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
I have also used the below command so i can control the copy format
for tablename in 'cat $current_path/table.txt(have defined the order)
do
for data in 'cat path/$tablename.txt
do
echo "$data" >> final/output.txt
done
done
here it is doing its work but generating multiple copies of the single txt as for loop is working .
linux shell-script
closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
I have also used the below command so i can control the copy format
for tablename in 'cat $current_path/table.txt(have defined the order)
do
for data in 'cat path/$tablename.txt
do
echo "$data" >> final/output.txt
done
done
here it is doing its work but generating multiple copies of the single txt as for loop is working .
linux shell-script
I have used the code cat *.txt >> output.txt . This code is doing is its work but here unix is deciding the order in which the files are being copied to my output file which is undesirable as i want the files to be copied according to their occurence or time of generation .
I have also used the below command so i can control the copy format
for tablename in 'cat $current_path/table.txt(have defined the order)
do
for data in 'cat path/$tablename.txt
do
echo "$data" >> final/output.txt
done
done
here it is doing its work but generating multiple copies of the single txt as for loop is working .
linux shell-script
linux shell-script
edited Jan 16 at 8:34
Rui F Ribeiro
39.8k1479133
39.8k1479133
asked Jan 16 at 3:22
Rahul RawatRahul Rawat
111
111
closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by G-Man, Mr Shunz, Thomas, X Tian, Luciano Andress Martini Jan 17 at 14:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Assuming I understand the question then
xargs < "$current_path/table.txt" cat > final/output.txt
is all you need. xargs
will read the "table.txt" file and run cat
with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs
executes cat
more than once, as the redirection is done only once before any cat
(or even the xargs
) is executed.
If you want it sorted according to time, then
ls -t | xargs cat > final/output
will work if there are no awkward filenames (e.g. embedded spaces or newlines).
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Doesls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data
– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does ther
in thels -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 filescat
,dog
andzebra
each of which just has a line sayingthis is cat
,this is dog
andthis is zebra
. Runtouch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output ofls -l
and also your desired output.
– icarus
Jan 16 at 18:35
add a comment |
for tablename in $(cat $current_path/table.txt);do
for word in $(cat path/$tablename.txt);do
echo $word
done
done > final/output.txt
This should work. I'm not sure what you mean by
here it is doing its work but generating multiple copies of the single txt as for loop is working
Can you be more specific ?
-- Edited -- following the suggestion below.
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for eachcat
) but also removes the need to "start with a clean file". So the code is justfor tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening theoutput.txt
file on each iteration.
– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming I understand the question then
xargs < "$current_path/table.txt" cat > final/output.txt
is all you need. xargs
will read the "table.txt" file and run cat
with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs
executes cat
more than once, as the redirection is done only once before any cat
(or even the xargs
) is executed.
If you want it sorted according to time, then
ls -t | xargs cat > final/output
will work if there are no awkward filenames (e.g. embedded spaces or newlines).
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Doesls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data
– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does ther
in thels -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 filescat
,dog
andzebra
each of which just has a line sayingthis is cat
,this is dog
andthis is zebra
. Runtouch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output ofls -l
and also your desired output.
– icarus
Jan 16 at 18:35
add a comment |
Assuming I understand the question then
xargs < "$current_path/table.txt" cat > final/output.txt
is all you need. xargs
will read the "table.txt" file and run cat
with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs
executes cat
more than once, as the redirection is done only once before any cat
(or even the xargs
) is executed.
If you want it sorted according to time, then
ls -t | xargs cat > final/output
will work if there are no awkward filenames (e.g. embedded spaces or newlines).
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Doesls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data
– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does ther
in thels -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 filescat
,dog
andzebra
each of which just has a line sayingthis is cat
,this is dog
andthis is zebra
. Runtouch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output ofls -l
and also your desired output.
– icarus
Jan 16 at 18:35
add a comment |
Assuming I understand the question then
xargs < "$current_path/table.txt" cat > final/output.txt
is all you need. xargs
will read the "table.txt" file and run cat
with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs
executes cat
more than once, as the redirection is done only once before any cat
(or even the xargs
) is executed.
If you want it sorted according to time, then
ls -t | xargs cat > final/output
will work if there are no awkward filenames (e.g. embedded spaces or newlines).
Assuming I understand the question then
xargs < "$current_path/table.txt" cat > final/output.txt
is all you need. xargs
will read the "table.txt" file and run cat
with the files in the specified order, so will output them to stdout, and you redirect stdout to the desired place. Note that this works even if there are so many files that xargs
executes cat
more than once, as the redirection is done only once before any cat
(or even the xargs
) is executed.
If you want it sorted according to time, then
ls -t | xargs cat > final/output
will work if there are no awkward filenames (e.g. embedded spaces or newlines).
answered Jan 16 at 4:11
icarusicarus
6,02111030
6,02111030
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Doesls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data
– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does ther
in thels -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 filescat
,dog
andzebra
each of which just has a line sayingthis is cat
,this is dog
andthis is zebra
. Runtouch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output ofls -l
and also your desired output.
– icarus
Jan 16 at 18:35
add a comment |
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Doesls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data
– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does ther
in thels -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 filescat
,dog
andzebra
each of which just has a line sayingthis is cat
,this is dog
andthis is zebra
. Runtouch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output ofls -l
and also your desired output.
– icarus
Jan 16 at 18:35
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Hi icarus the below command does work. Ls - t | xargs cat > final/output but there is one problem that is it is copying the last generated txt file on the top but i want the file which is generated first should be copies to the output file first then the following ones. For example if there are abc.txt (first files) abd.txt then the output file should have data firat of abc..txt and the the following files. Can this be done
– Rahul Rawat
Jan 16 at 8:37
Does
ls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data– icarus
Jan 16 at 12:53
Does
ls -rt | grep -v output |xargs cat > final/output
do what you want? Can you edit your question to put in some more example data– icarus
Jan 16 at 12:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
I mean i want to append the data in the output file according to the generatiin of the file by using above code the. The output file generated show the data of the last file and then the first it is reversing the order of appending data that is last showing first and first files data showing at the end
– Rahul Rawat
Jan 16 at 13:53
Does the
r
in the ls -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat
, dog
and zebra
each of which just has a line saying this is cat
, this is dog
and this is zebra
. Run touch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l
and also your desired output.– icarus
Jan 16 at 18:35
Does the
r
in the ls -rt
not do what you want? It appears that both I and @joolin are having difficulty in understanding what you are asking for. Please edit your question to show some example data. For example create 3 files cat
, dog
and zebra
each of which just has a line saying this is cat
, this is dog
and this is zebra
. Run touch dog
so dog is the newest file so the ages of the files do not match the alphabetical order. show us the output of ls -l
and also your desired output.– icarus
Jan 16 at 18:35
add a comment |
for tablename in $(cat $current_path/table.txt);do
for word in $(cat path/$tablename.txt);do
echo $word
done
done > final/output.txt
This should work. I'm not sure what you mean by
here it is doing its work but generating multiple copies of the single txt as for loop is working
Can you be more specific ?
-- Edited -- following the suggestion below.
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for eachcat
) but also removes the need to "start with a clean file". So the code is justfor tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening theoutput.txt
file on each iteration.
– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
add a comment |
for tablename in $(cat $current_path/table.txt);do
for word in $(cat path/$tablename.txt);do
echo $word
done
done > final/output.txt
This should work. I'm not sure what you mean by
here it is doing its work but generating multiple copies of the single txt as for loop is working
Can you be more specific ?
-- Edited -- following the suggestion below.
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for eachcat
) but also removes the need to "start with a clean file". So the code is justfor tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening theoutput.txt
file on each iteration.
– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
add a comment |
for tablename in $(cat $current_path/table.txt);do
for word in $(cat path/$tablename.txt);do
echo $word
done
done > final/output.txt
This should work. I'm not sure what you mean by
here it is doing its work but generating multiple copies of the single txt as for loop is working
Can you be more specific ?
-- Edited -- following the suggestion below.
for tablename in $(cat $current_path/table.txt);do
for word in $(cat path/$tablename.txt);do
echo $word
done
done > final/output.txt
This should work. I'm not sure what you mean by
here it is doing its work but generating multiple copies of the single txt as for loop is working
Can you be more specific ?
-- Edited -- following the suggestion below.
edited Jan 16 at 14:23
answered Jan 16 at 10:30
jayooinjayooin
3347
3347
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for eachcat
) but also removes the need to "start with a clean file". So the code is justfor tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening theoutput.txt
file on each iteration.
– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
add a comment |
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for eachcat
) but also removes the need to "start with a clean file". So the code is justfor tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening theoutput.txt
file on each iteration.
– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each
cat
) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
This is a correct answer, but it can be improved. One of the things that you can do in the shell is redirect control structures. In this case you can redirect the for loop to the file. Doing so is fractionally more efficient (the shell doesn't need to do the redirection for each
cat
) but also removes the need to "start with a clean file". So the code is just for tablename in $(cat $current_path/table.txt); do cat "$tablename" ; done > final/output.txt
– icarus
Jan 16 at 13:00
True, it avoids opening the
output.txt
file on each iteration.– jayooin
Jan 16 at 13:25
True, it avoids opening the
output.txt
file on each iteration.– jayooin
Jan 16 at 13:25
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi have tried this as in my code using the table.txt i am creating seperate $tablename.txt as i have code above also which created diffrent txt. So if you will cat $tablename.txt it will throw error as each $tablename.txt has date row by row and not in a single row ex asdfff gsss. Gsss so here cat wont do.
– Rahul Rawat
Jan 16 at 14:02
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
Hi, I edited the answer following your comment. Basically you need the words in each file to be displayed as lines. I'm not sure about the order
– jayooin
Jan 16 at 14:25
add a comment |