Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cat start a new line

Tags:

bash

unix

I have four files:

one_file.txt

abc | def

two_file.txt

ghi | jkl

three_file.txt

mno | pqr

four_WORD.txt

xyz| xyz

I want to concatenate all of the files ending with "file.txt" (i.e. all except four_WORD.txt) in order to get:

abc | def
ghi | jkl
mno | pqr

To accomplish this, I run:

cat *file.txt > full_set.txt

However, full_set.txt comes out as:

abc | defmno | pqrghi | jkl

Any ideas how to do this correctly and efficiently so that each ends up on its own line? In reality, I need to do the above for a lot of very large files. Thank you in advance for your help.

like image 434
user3890260 Avatar asked Jul 30 '14 06:07

user3890260


2 Answers

Try:

awk 1 *file.txt > full_set.txt

This is less efficient than a bare cat but will add an extra \n if missing at the end of each file

like image 110
Sylvain Leroux Avatar answered Sep 23 '22 15:09

Sylvain Leroux


Many tools will add newlines if they are missing. Try e.g.

sed '' *file.txt >full_set.txt

but this depends on your sed version. Others to try include Awk, grep -ho '.*' file*.txt and etc.

like image 30
tripleee Avatar answered Sep 22 '22 15:09

tripleee