Merge text files next to each other

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite












I have .txt data from spectrometer with 800 lines and 5 columns. And I want to merge about 15 these files next to each other. So final .txt file should have 800 lines and 75 columns.



All .txt files looks like this:



Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734
201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103
...


Desired file:



Wave ;Sample ;Dark ;Reference;Absorbance; Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]; [nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734; 200.00; 34.647; -0.317; 79.668; 0.35734;
201.00; 29.937; -21.220; 70.385; 0.25357; 201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721; 202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103; 203.00; 43.201; 4.165; 136.775; 0.53103


I tried:



cat *.txt > file.txt


But this add .txt files like new lines not like new columns.










share|improve this question



















  • 2




    Add some example input files and your desired output from those.
    – heemayl
    Mar 9 at 9:02






  • 1




    If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
    – sudodus
    Mar 9 at 9:31














up vote
1
down vote

favorite












I have .txt data from spectrometer with 800 lines and 5 columns. And I want to merge about 15 these files next to each other. So final .txt file should have 800 lines and 75 columns.



All .txt files looks like this:



Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734
201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103
...


Desired file:



Wave ;Sample ;Dark ;Reference;Absorbance; Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]; [nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734; 200.00; 34.647; -0.317; 79.668; 0.35734;
201.00; 29.937; -21.220; 70.385; 0.25357; 201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721; 202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103; 203.00; 43.201; 4.165; 136.775; 0.53103


I tried:



cat *.txt > file.txt


But this add .txt files like new lines not like new columns.










share|improve this question



















  • 2




    Add some example input files and your desired output from those.
    – heemayl
    Mar 9 at 9:02






  • 1




    If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
    – sudodus
    Mar 9 at 9:31












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have .txt data from spectrometer with 800 lines and 5 columns. And I want to merge about 15 these files next to each other. So final .txt file should have 800 lines and 75 columns.



All .txt files looks like this:



Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734
201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103
...


Desired file:



Wave ;Sample ;Dark ;Reference;Absorbance; Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]; [nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734; 200.00; 34.647; -0.317; 79.668; 0.35734;
201.00; 29.937; -21.220; 70.385; 0.25357; 201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721; 202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103; 203.00; 43.201; 4.165; 136.775; 0.53103


I tried:



cat *.txt > file.txt


But this add .txt files like new lines not like new columns.










share|improve this question















I have .txt data from spectrometer with 800 lines and 5 columns. And I want to merge about 15 these files next to each other. So final .txt file should have 800 lines and 75 columns.



All .txt files looks like this:



Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734
201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103
...


Desired file:



Wave ;Sample ;Dark ;Reference;Absorbance; Wave ;Sample ;Dark ;Reference;Absorbance
[nm] ;[counts] ;[counts] ;[counts] ;[A.U]; [nm] ;[counts] ;[counts] ;[counts] ;[A.U]
200.00; 34.647; -0.317; 79.668; 0.35734; 200.00; 34.647; -0.317; 79.668; 0.35734;
201.00; 29.937; -21.220; 70.385; 0.25357; 201.00; 29.937; -21.220; 70.385; 0.25357
202.00; 48.826; -0.527; 99.657; 0.30721; 202.00; 48.826; -0.527; 99.657; 0.30721
203.00; 43.201; 4.165; 136.775; 0.53103; 203.00; 43.201; 4.165; 136.775; 0.53103


I tried:



cat *.txt > file.txt


But this add .txt files like new lines not like new columns.







command-line text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 10 at 0:44









galoget

2,1062820




2,1062820










asked Mar 9 at 8:55









Adam Srut

83




83







  • 2




    Add some example input files and your desired output from those.
    – heemayl
    Mar 9 at 9:02






  • 1




    If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
    – sudodus
    Mar 9 at 9:31












  • 2




    Add some example input files and your desired output from those.
    – heemayl
    Mar 9 at 9:02






  • 1




    If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
    – sudodus
    Mar 9 at 9:31







2




2




Add some example input files and your desired output from those.
– heemayl
Mar 9 at 9:02




Add some example input files and your desired output from those.
– heemayl
Mar 9 at 9:02




1




1




If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
– sudodus
Mar 9 at 9:31




If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet.
– sudodus
Mar 9 at 9:31










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You’re looking for the paste utility, e.g.



paste *.txt > file.txt


Check its manual page for details, most notably the -d option to specify the delimiter.






share|improve this answer




















  • I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
    – Adam Srut
    Mar 9 at 10:15










  • @AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
    – sudodus
    Mar 9 at 16:44











  • sorry sdiff works only on two files... I edited my question.
    – Adam Srut
    Mar 9 at 17:45

















up vote
3
down vote













Simple example



I set the separator to 'space' and eliminate multiple spaces.



$ cat file-1
ett tu tre
1 2 3
11 22 33
$ cat file-2
fyra fem sex
4 5 6
44 55 66

$ cat file-3
sju atta nio
7 8 9
77 88 99

$ paste file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-? | tr -s ' ' ' '
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99


Suggested command line



Maybe you want to eliminate the spaces? You want to keep semicolon as separator. Try with the following command line



paste -d ';' input-filespec-with-wildcard | sed 's/ //g' > output-file


where input-filespec-with-wildcard could be *.txt (if the files to be merged are the only txt files in the directory).



Edit (using the real data)



  • Convert the files to unix/linux text format, CarriageReturn + LineFeed --> LineFeed with dos2unix



  • Remove blank lines with the following command



    for i in *.TXT;do echo sed '/^$/'d "$i" > "$i/.TXT/.txt";done



  • Remove header lines with the following command



    for i in *.txt;do grep ';*;' "$i" > "$i/.txt/.txt1";done


    (Header lines do not contain at least two semicolons.)




  • Merge the files with the following command



    paste -d ';' *.txt1 | sed 's/ //g' > merged-file.txt


I read the merged file into LibreOffice Calc and used semicolon as separator. It looks like I think you want to have it. I hope it works for you according to these instructions. Otherwise please describe your remaining problem and tell me if you want me to upload 'my' merged-file.txt.



LibreOffice Calc



If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet. You can edit the positions in the spreadsheet and remove the lines, that create confusion.



I would say that it is rather easy to do it manually in LibreOffice Calc, but if this is something that you will do many times or with a great number of files, it is better to use and maybe refine the command line method.






share|improve this answer






















  • That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
    – Adam Srut
    Mar 12 at 21:38











  • You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
    – sudodus
    Mar 13 at 7:06










  • But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
    – sudodus
    Mar 13 at 7:12










  • Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
    – Adam Srut
    Mar 13 at 8:33










  • @AdamSrut, Have a look at what I think is a working solution.
    – sudodus
    Mar 13 at 11:02










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1013344%2fmerge-text-files-next-to-each-other%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










You’re looking for the paste utility, e.g.



paste *.txt > file.txt


Check its manual page for details, most notably the -d option to specify the delimiter.






share|improve this answer




















  • I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
    – Adam Srut
    Mar 9 at 10:15










  • @AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
    – sudodus
    Mar 9 at 16:44











  • sorry sdiff works only on two files... I edited my question.
    – Adam Srut
    Mar 9 at 17:45














up vote
4
down vote



accepted










You’re looking for the paste utility, e.g.



paste *.txt > file.txt


Check its manual page for details, most notably the -d option to specify the delimiter.






share|improve this answer




















  • I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
    – Adam Srut
    Mar 9 at 10:15










  • @AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
    – sudodus
    Mar 9 at 16:44











  • sorry sdiff works only on two files... I edited my question.
    – Adam Srut
    Mar 9 at 17:45












up vote
4
down vote



accepted







up vote
4
down vote



accepted






You’re looking for the paste utility, e.g.



paste *.txt > file.txt


Check its manual page for details, most notably the -d option to specify the delimiter.






share|improve this answer












You’re looking for the paste utility, e.g.



paste *.txt > file.txt


Check its manual page for details, most notably the -d option to specify the delimiter.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 9:42









egmont

3,5691821




3,5691821











  • I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
    – Adam Srut
    Mar 9 at 10:15










  • @AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
    – sudodus
    Mar 9 at 16:44











  • sorry sdiff works only on two files... I edited my question.
    – Adam Srut
    Mar 9 at 17:45
















  • I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
    – Adam Srut
    Mar 9 at 10:15










  • @AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
    – sudodus
    Mar 9 at 16:44











  • sorry sdiff works only on two files... I edited my question.
    – Adam Srut
    Mar 9 at 17:45















I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
– Adam Srut
Mar 9 at 10:15




I'm probably looking for sdiff. But I don't know what option use. Each file should be delimited with ";" but there is "tab |".
– Adam Srut
Mar 9 at 10:15












@AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
– sudodus
Mar 9 at 16:44





@AdamSrut, Please explain with much more details, what you want! Edit your original question to add these details.
– sudodus
Mar 9 at 16:44













sorry sdiff works only on two files... I edited my question.
– Adam Srut
Mar 9 at 17:45




sorry sdiff works only on two files... I edited my question.
– Adam Srut
Mar 9 at 17:45












up vote
3
down vote













Simple example



I set the separator to 'space' and eliminate multiple spaces.



$ cat file-1
ett tu tre
1 2 3
11 22 33
$ cat file-2
fyra fem sex
4 5 6
44 55 66

$ cat file-3
sju atta nio
7 8 9
77 88 99

$ paste file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-? | tr -s ' ' ' '
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99


Suggested command line



Maybe you want to eliminate the spaces? You want to keep semicolon as separator. Try with the following command line



paste -d ';' input-filespec-with-wildcard | sed 's/ //g' > output-file


where input-filespec-with-wildcard could be *.txt (if the files to be merged are the only txt files in the directory).



Edit (using the real data)



  • Convert the files to unix/linux text format, CarriageReturn + LineFeed --> LineFeed with dos2unix



  • Remove blank lines with the following command



    for i in *.TXT;do echo sed '/^$/'d "$i" > "$i/.TXT/.txt";done



  • Remove header lines with the following command



    for i in *.txt;do grep ';*;' "$i" > "$i/.txt/.txt1";done


    (Header lines do not contain at least two semicolons.)




  • Merge the files with the following command



    paste -d ';' *.txt1 | sed 's/ //g' > merged-file.txt


I read the merged file into LibreOffice Calc and used semicolon as separator. It looks like I think you want to have it. I hope it works for you according to these instructions. Otherwise please describe your remaining problem and tell me if you want me to upload 'my' merged-file.txt.



LibreOffice Calc



If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet. You can edit the positions in the spreadsheet and remove the lines, that create confusion.



I would say that it is rather easy to do it manually in LibreOffice Calc, but if this is something that you will do many times or with a great number of files, it is better to use and maybe refine the command line method.






share|improve this answer






















  • That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
    – Adam Srut
    Mar 12 at 21:38











  • You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
    – sudodus
    Mar 13 at 7:06










  • But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
    – sudodus
    Mar 13 at 7:12










  • Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
    – Adam Srut
    Mar 13 at 8:33










  • @AdamSrut, Have a look at what I think is a working solution.
    – sudodus
    Mar 13 at 11:02














up vote
3
down vote













Simple example



I set the separator to 'space' and eliminate multiple spaces.



$ cat file-1
ett tu tre
1 2 3
11 22 33
$ cat file-2
fyra fem sex
4 5 6
44 55 66

$ cat file-3
sju atta nio
7 8 9
77 88 99

$ paste file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-? | tr -s ' ' ' '
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99


Suggested command line



Maybe you want to eliminate the spaces? You want to keep semicolon as separator. Try with the following command line



paste -d ';' input-filespec-with-wildcard | sed 's/ //g' > output-file


where input-filespec-with-wildcard could be *.txt (if the files to be merged are the only txt files in the directory).



Edit (using the real data)



  • Convert the files to unix/linux text format, CarriageReturn + LineFeed --> LineFeed with dos2unix



  • Remove blank lines with the following command



    for i in *.TXT;do echo sed '/^$/'d "$i" > "$i/.TXT/.txt";done



  • Remove header lines with the following command



    for i in *.txt;do grep ';*;' "$i" > "$i/.txt/.txt1";done


    (Header lines do not contain at least two semicolons.)




  • Merge the files with the following command



    paste -d ';' *.txt1 | sed 's/ //g' > merged-file.txt


I read the merged file into LibreOffice Calc and used semicolon as separator. It looks like I think you want to have it. I hope it works for you according to these instructions. Otherwise please describe your remaining problem and tell me if you want me to upload 'my' merged-file.txt.



LibreOffice Calc



If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet. You can edit the positions in the spreadsheet and remove the lines, that create confusion.



I would say that it is rather easy to do it manually in LibreOffice Calc, but if this is something that you will do many times or with a great number of files, it is better to use and maybe refine the command line method.






share|improve this answer






















  • That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
    – Adam Srut
    Mar 12 at 21:38











  • You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
    – sudodus
    Mar 13 at 7:06










  • But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
    – sudodus
    Mar 13 at 7:12










  • Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
    – Adam Srut
    Mar 13 at 8:33










  • @AdamSrut, Have a look at what I think is a working solution.
    – sudodus
    Mar 13 at 11:02












up vote
3
down vote










up vote
3
down vote









Simple example



I set the separator to 'space' and eliminate multiple spaces.



$ cat file-1
ett tu tre
1 2 3
11 22 33
$ cat file-2
fyra fem sex
4 5 6
44 55 66

$ cat file-3
sju atta nio
7 8 9
77 88 99

$ paste file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-? | tr -s ' ' ' '
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99


Suggested command line



Maybe you want to eliminate the spaces? You want to keep semicolon as separator. Try with the following command line



paste -d ';' input-filespec-with-wildcard | sed 's/ //g' > output-file


where input-filespec-with-wildcard could be *.txt (if the files to be merged are the only txt files in the directory).



Edit (using the real data)



  • Convert the files to unix/linux text format, CarriageReturn + LineFeed --> LineFeed with dos2unix



  • Remove blank lines with the following command



    for i in *.TXT;do echo sed '/^$/'d "$i" > "$i/.TXT/.txt";done



  • Remove header lines with the following command



    for i in *.txt;do grep ';*;' "$i" > "$i/.txt/.txt1";done


    (Header lines do not contain at least two semicolons.)




  • Merge the files with the following command



    paste -d ';' *.txt1 | sed 's/ //g' > merged-file.txt


I read the merged file into LibreOffice Calc and used semicolon as separator. It looks like I think you want to have it. I hope it works for you according to these instructions. Otherwise please describe your remaining problem and tell me if you want me to upload 'my' merged-file.txt.



LibreOffice Calc



If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet. You can edit the positions in the spreadsheet and remove the lines, that create confusion.



I would say that it is rather easy to do it manually in LibreOffice Calc, but if this is something that you will do many times or with a great number of files, it is better to use and maybe refine the command line method.






share|improve this answer














Simple example



I set the separator to 'space' and eliminate multiple spaces.



$ cat file-1
ett tu tre
1 2 3
11 22 33
$ cat file-2
fyra fem sex
4 5 6
44 55 66

$ cat file-3
sju atta nio
7 8 9
77 88 99

$ paste file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-?
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99

$ paste -d ' ' file-? | tr -s ' ' ' '
ett tu tre fyra fem sex sju atta nio
1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99


Suggested command line



Maybe you want to eliminate the spaces? You want to keep semicolon as separator. Try with the following command line



paste -d ';' input-filespec-with-wildcard | sed 's/ //g' > output-file


where input-filespec-with-wildcard could be *.txt (if the files to be merged are the only txt files in the directory).



Edit (using the real data)



  • Convert the files to unix/linux text format, CarriageReturn + LineFeed --> LineFeed with dos2unix



  • Remove blank lines with the following command



    for i in *.TXT;do echo sed '/^$/'d "$i" > "$i/.TXT/.txt";done



  • Remove header lines with the following command



    for i in *.txt;do grep ';*;' "$i" > "$i/.txt/.txt1";done


    (Header lines do not contain at least two semicolons.)




  • Merge the files with the following command



    paste -d ';' *.txt1 | sed 's/ //g' > merged-file.txt


I read the merged file into LibreOffice Calc and used semicolon as separator. It looks like I think you want to have it. I hope it works for you according to these instructions. Otherwise please describe your remaining problem and tell me if you want me to upload 'my' merged-file.txt.



LibreOffice Calc



If you want to do it only once, you can do manually/visually in Libre Office Calc, by pasting the files into the spreadsheet. You can edit the positions in the spreadsheet and remove the lines, that create confusion.



I would say that it is rather easy to do it manually in LibreOffice Calc, but if this is something that you will do many times or with a great number of files, it is better to use and maybe refine the command line method.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 13 at 11:00

























answered Mar 9 at 18:17









sudodus

20.4k32668




20.4k32668











  • That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
    – Adam Srut
    Mar 12 at 21:38











  • You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
    – sudodus
    Mar 13 at 7:06










  • But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
    – sudodus
    Mar 13 at 7:12










  • Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
    – Adam Srut
    Mar 13 at 8:33










  • @AdamSrut, Have a look at what I think is a working solution.
    – sudodus
    Mar 13 at 11:02
















  • That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
    – Adam Srut
    Mar 12 at 21:38











  • You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
    – sudodus
    Mar 13 at 7:06










  • But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
    – sudodus
    Mar 13 at 7:12










  • Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
    – Adam Srut
    Mar 13 at 8:33










  • @AdamSrut, Have a look at what I think is a working solution.
    – sudodus
    Mar 13 at 11:02















That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
– Adam Srut
Mar 12 at 21:38





That is almost it! But I get line from each file under line from previous file as a result. So I tried to eliminate end of lines "n" and I type | sed 's/n//g' but that does not work.
– Adam Srut
Mar 12 at 21:38













You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
– sudodus
Mar 13 at 7:06




You can try tr 'n' ';' to convert the unwanted newlines to semicolons (which you use as separators). But it will remove all newlines, and that is probably not what you want. If you have blank lines (two newlines next to each other), you can try sed '/^$/'d where ^ marks the beginning of a line and $ marks the end of a line and d tells sed to delete the line (the blank line).
– sudodus
Mar 13 at 7:06












But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
– sudodus
Mar 13 at 7:12




But I am guessing. You can make it easier to help, if you upload some of the real files, that you want to merge and post a link to them. Then I can develop a method, that works for the real case. (If the files are secret, I can give you my public key for gpg encryption so that you can make them readable only for me.)
– sudodus
Mar 13 at 7:12












Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
– Adam Srut
Mar 13 at 8:33




Yes I understand. Here is link to the files <vscht-my.sharepoint.com/:f:/g/personal/sruta_vscht_cz/…;
– Adam Srut
Mar 13 at 8:33












@AdamSrut, Have a look at what I think is a working solution.
– sudodus
Mar 13 at 11:02




@AdamSrut, Have a look at what I think is a working solution.
– sudodus
Mar 13 at 11:02

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1013344%2fmerge-text-files-next-to-each-other%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491