Home > Back-end >  Linux merging files
Linux merging files

Time:03-27

I'm doing a linux online course but im stuck with a question, you can find the question below.

You will get three files called a.bf, b.bf and c.bf. Merge the contents of these three files and write it to a new file called abc.bf. Respect the order: abc.bf must contain the contents of a.bf first, followed by those of b.bf, followed by those of c.bf.

Example Suppose the given files have the following contents:

a.bf contains .

b.bf contains [][][][].

c.bf contains <><><>.

The file abc.bf should then have

   [][][][]<><><>

as its content. I know how to merge the 3 files but when i use cat my output is:

   
[][][]
<><><>

When i use paste my output is " 'a lot of spaces' [][][][] 'a lot of spaces' <><><>"

My output that i need is [][][][]<><><>, i dont want the spaces between the content. Can someone help me?

CodePudding user response:

What you want to do is delete the newline characters.

With tr:

cat {a,b,c}.bf | tr --delete '\n' > abc.bf

With echo & sed:

echo $(cat {a,b,c}.bf) | sed -E 's/ //g' > abc.bf

With xargs & sed:

<{a,b,c}.bf xargs | sed -E 's/ //g' > abc.bf

Note that sed is only used to remove the spaces.

With cat & sed:

cat {a,b,c}.bf | sed -z 's/\n//g'

CodePudding user response:

echo -n "$(cat a.bf)$(cat b.bf)$(cat c.bf)" > abc.bf

  • echo -n will not output trailing newlines
  • Related