Skip to content
Advertisement

How to get a single output file with N lines from N multiline files?

I am looking for some effective way to concatenate multiple multiline files into one file – here is an example for three input files:

1.txt:

a b
c d

2.txt:

e f
g 
h 

3.txt:

ijklmn

output.txt:

a b c d
e f g h 
ijklmn

(Replacing each linebreak with single whitespace). Which way can you recommend?

Advertisement

Answer

Using BASH for loop:

for i in [0-9]*.txt; do tr 'n' ' ' < "$i"; echo; done > output.txt

cat output.txt
a b c d
e f g h
ijklmn

If you want to strip one ending space before each line break then use:

for i in [0-9]*.txt; do tr 'n' ' ' < "$i"; echo; done | sed 's/ *$//' > output.txt
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement