EstRaj
EstRaj

Reputation: 11

Shell Script for merging multiple text files into One file fails

Need help in merging multiple text files in to one file,

When am doing this through shell script it is changing the alignment of the file.

Eg : File 1 has data as below :

Hello world
Hello World1
Hello World2

File 2 has data as below :

Hello New World
Hello New World 2

Resultant file created through shell script post merging :

Hello world Hello Wor
ld Hello world2

the lines of the files are clubbed together.

This shell script is executed on the AS400 system

Code used :

cat *.${3} >> ${2}

Upvotes: 0

Views: 68

Answers (2)

mahes wari
mahes wari

Reputation: 91

Try this..

cat file1

`Hello world
Hello World1
Hello World2`

cat file1 >> file3

cat file3

Hello world
Hello World1
Hello World2

cat file2

Hello New World
Hello New World 2

cat file2 >> file3

cat file3

Hello world
Hello World1
Hello World2
Hello New World
Hello New World 2

Another way out is :$ sed -n wfile.merge file1.txt file2.txt

Upvotes: 1

Vincent
Vincent

Reputation: 195

I will assume that :

1 - ${3} is the extension of the files you want to concatenate, and therefore *.${3} is the names of all the source files.

2 - The result you posted is not actually an exact output. (there is no "world2" word in the source files)

What comes to mind is that the line terminations of the source files might be causing the error. Perhaps some '\r' are present there ? You should check that the line endings are unix-style (lines end with '\n')

Otherwise, try running cat File1 and cat File2 and check that the output is as expected. This should give you a hint as to why they are catted wrong

Upvotes: 0

Related Questions