13

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.

good_evening
  • 21,085
  • 65
  • 193
  • 298
  • 1
    does it? my cat (on cygwin) doesn't put any extra new lines before or after any of 2 files. Are you sure you don't have the new line in file1 or file2? – Kashyap Oct 12 '11 at 20:18
  • 1
    Can you clarify what `file1` and `file2` look like, and just what output you want in `file3`? Exactly what new lines do you want in the output, and what new lines don't you want? – Keith Thompson Oct 12 '11 at 21:16

4 Answers4

23

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"
Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
2

in bash, you can do:

cat <(sed -n '1n;p' file1) <(sed -n '1n;p' file2)
Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
0

Try this:

user$ echo hi1 > file1       #for example
user$ echo hi2 > file2       #for example
user$ x=$(cat file1 file2)
user$ echo -n $x > file3
user$ cat file3
hi1 hi2                      #no newline is added.
Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
-1

you ca use awk:

awk '(FNR>1){print}' file1 file2

update - how it works:

we ask awk to process two files: file1 and file2. It will print whole record (line) if condition (FNR>1) if true. FNR is a variable defined as:

FNR - The input record number in the current input file.

so, condition (FNR>1) will be true every time, except for the first line of each file. This way we skip first line of each file.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85