Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two files with cat without new line

Tags:

linux

shell

I want to merge two files cat file1 file2 > file3. But it starts with new line. I don't want that. I could use tr to replace all new lines to space, but I can't do that, because there are new lines in files which I don't want to replace.

like image 914
good_evening Avatar asked Oct 12 '11 20:10

good_evening


2 Answers

You could use head with -1 as the -c flags parameter and -q

head -c -1 -q file1 file2 > file3

head -c -1 will output everything up to the last 1 byte of the code (in this case the last 1 byte - endline - wont be included). The -q is so the filenames dont get piped to file3 as head does by default when heading multiple files.


Or, as suggested by this answer - bash cat multiple files content in to single string without newlines , pipe it to tr:

tr -d "\n"
like image 53
chown Avatar answered Oct 08 '22 10:10

chown


in bash, you can do:

cat <(sed -n '1n;p' file1) <(sed -n '1n;p' file2)
like image 22
Michał Šrajer Avatar answered Oct 08 '22 10:10

Michał Šrajer