Merge multiple columns into one using cat


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Question Merge multiple columns into one using cat

I will like to merge several files using 'cat', but I observe the output is not consistent. the merge begins at the last line of the first file.
Code:
file1.txt:
1234
1234
1234

file2.txt:
aaaa
bbbb
cccc
dddd

cat file1.txt file2.txt > file3.txt
file3.txt:
1234
1234
1234aaaa
bbbb
cccc
dddd

Could this be an error associated with the command or a bug?
Thanks.
# 2  
Hello geomarine,

It is working well and fine for me and moreover I am getting different Input_file(s) output from new line only as follows.

Code:
 
cat file1.TXT file2.TXT > file3.TXT
cat file3.TXT
1234
1234
1234
aaaa
bbbb
cccc
dddd

Could you please check once if your Input_file is having carriage returns in it? By doing cat -v Input_file if yes then kindly remove them from following command.

Code:
 
tr -d '\r' < Input_file > temp_file && mv temp_file Input_file

Let us know how it goes then, I hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Probably the last line does not have a closing linefeed character. Proper Unix files are required to have this..

See if this works:
Code:
echo | cat file1.txt - file2.txt > file3.txt

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Thanks RavinderSingh13 and Scrutinizer . I check to see if there are carriage returns with:
Code:
cat -v Input_file

result:
Code:
cat -v file3.txt 
1234^M
1234^M
1234aaaa^M
bbbb^M
cccc^M
dddd

using this command suggested:
Code:
tr -d '\r' < file1.txt > temp_file && mv temp_file file1.txt

the carriage return was removed. However, the result is still same:
Code:
1234
1234
1234aaaa
bbbb
cccc
dddd

when I use this command:
Code:
echo | cat file1.txt - file2.txt > file3.txt

the result is:
Code:
123412341234aaaabbbbccccdddd

I am using BASH shell.

Last edited by Scrutinizer; 05-27-2018 at 12:51 PM.. Reason: code tags
# 5  
What OS are you using?
# 6  
Windows 7 Professional
# 7  
You have shown us that you have two input files with <carriage-return><new-line> line separators and no final line terminators (i.e., DOS text file format).

You have shown us several outputs that you have gotten from what you originally tried and form what others have suggested and you have told us that none of them are what you want. But, from your description and from your examples, I don't know whether you're trying to get all lines from each input file combined into single lines (one for each input file) in the output, all input lines from all files combined into a single output line (sequentially or pairwise), pairs of lines (one from each input file) combined into output lines, or something else.

Please show us the output you are hoping to get from your sample input files and tell us whether you want that output to be in DOS text file format (like your input files) or in standard UNIX text file format (i.e. <new-line> line terminators on every line with no <carriage-return> characters).
This User Gave Thanks to Don Cragun For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #888
Difficulty: Easy
sed was developed from 1973 to 1974 by Bill Gates of Microsoft.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join and merge multiple files with duplicate key and fill void columns

Join and merge multiple files with duplicate key and fill void columns Hi guys, I have many files that I want to merge: file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: (5 Replies)
Discussion started by: yjacknewton
5 Replies

2. Shell Programming and Scripting

Merge columns from multiple files

Hello and Good day I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I need to merge $2,$3,$6 from first file and $6 from another files. File1: $1 $2 $3 $4 $5 $6... (8 Replies)
Discussion started by: ali.seifaddini
8 Replies

3. Shell Programming and Scripting

Merge records based on multiple columns

Hi, I have a file with 16 columns and out of these 16 columns 14 are key columns, 15 th is order column and 16th column is having information. I need to concate the 16th column based on value of 1-14th column as key in order of 15th column. Here are the example file Input File (multiple... (3 Replies)
Discussion started by: Ravi Agrawal
3 Replies

4. UNIX for Dummies Questions & Answers

Merge columns from multiple files

Hi all, I've searched the web for a long time trying to figure out how to merge columns from multiple files. I know paste will append columns like so: paste file1 file2 file3 file4 file5 ... But this becomes inconvenient when you want to append a large number of files into a single file. ... (2 Replies)
Discussion started by: torchij
2 Replies

5. UNIX for Dummies Questions & Answers

How do I merge multiple columns into one column?

Hi all, I'm looking for a way to merge multiple columns (from one file) into a single column in an output file. The file I have looks somewhat like this: @HWI-ST212 1:N:0 AGTCCTACCGGGAGT + @@@DDDDDHHHHHII @HWI-ST212 1:N:0 CGTTTAAAAATTTCT + @;@B;DDDDH?:F;F... (4 Replies)
Discussion started by: Vnguyen
4 Replies

6. Shell Programming and Scripting

Match on first and last columns and merge

Hi Friends, I have an input file of this kind input.txt output.txt The logic is as follows, read a row, compare column1 to the next, if there is a match, compare column4. If these two match, grab the minimum of column2 and maximum of column3 and print the output with column1,... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

Merge columns

Hi all - I have a file like below: A: A1,A2,A3,A4 B: 1,2,3,4 C: z,y,x,w .... This format repeats The output should come in a single line merging the first line field with the other two rows: A1_1 A1_z A2_2 A2_y A3_3 A3_x A4_4 A4_w Could anyone help with some directions ... (4 Replies)
Discussion started by: deepakgang
4 Replies

8. Shell Programming and Scripting

merge the two files which has contain columns

Hi may i ask how to accomplish this task: I have 2 files which has multiple columns first file 1 a 2 b 3 c 4 d second file 14 a 9 .... 13 b 10.... 12 c 11... 11 d 12... I want to merge the second file to first file that will looks like this ... (2 Replies)
Discussion started by: jao_madn
2 Replies

9. Shell Programming and Scripting

How to merge rows into columns ????

Hi guz I want to merge multiple rows into a multiple columns based on the first column. The file has symbol // I want to break the symbool // and I nedd exactlynew column at that point the output will be like this please guyz help in this isssue!!!!! merging rows into columns ... (4 Replies)
Discussion started by: bogu0001
4 Replies

10. UNIX for Advanced & Expert Users

use of sed over cat to merge files

I have three files, basically: file 1 - one line header file 2 - big data (approx 80GB) file 3 - a one line trailer the existing process cats these together i.e cat file 1 file 2 file 3 however... I was thinking, surely it could be more efficient to insert the header (file 1) on the... (2 Replies)
Discussion started by: miwinter
2 Replies

Featured Tech Videos