10

I have two files:

k.txt:

3 5 7 9 19 20 

h.txt:

000010
100001
111001

if I just use cat, there is no newline. I need a command which would provide a file which looks like this:

3 5 7 9 19 20 
000010
100001
111001
4
  • 2
    What newline? How do you use cat? cat k.txt h.txt > newfile will create exactly the output you show.
    – terdon
    Jun 23, 2015 at 17:11
  • 3
    @terdon unless the file itself is does not have its terminating newline... ? Jun 23, 2015 at 17:12
  • 7
    My amazing psychic powers tell me that the file contents as given in the question are not the actual file contents.
    – JdeBP
    Jun 23, 2015 at 17:12
  • Given askubuntu.com/questions/614271 it becomes clear that the real files concerned are actually the (post-processed) output of a evolutionary biology simulation program named ms (doco).
    – JdeBP
    Jun 23, 2015 at 20:20

7 Answers 7

22

If, as steeldriver suggests, your files don't end with a newline, you could try:

awk '{print}' k.txt h.txt > newfile

or, if you like golfing

awk 1 k.txt h.txt > newfile

or

perl -lne 'print' k.txt h.txt 

or

( cat k.txt ; echo ""; cat h.txt; echo ) > newfile

or

echo "$(cat k.txt)"; echo "$(cat h.txt)"
7
  • 2
    I want to edit that to awk 1 just for how it will look. :D
    – muru
    Jun 23, 2015 at 17:21
  • @muru heh, done :)
    – terdon
    Jun 23, 2015 at 17:26
  • The file has 127 MB ;)
    – A.B.
    Jun 23, 2015 at 17:28
  • Common, awk, perl, cat. And me?
    – A.B.
    Jun 23, 2015 at 17:29
  • 2
    +1. IMO the last answer is the best: the command substitution $(cat file) will strip any trailing newlines, and echo will explicitly add one. FYI, bash has a builtin for $(cat file) ==> $(<file) -- save a few microseconds. And with one command: printf "%s\n" "$(<k.txt)" "$(<h.txt)" Jun 23, 2015 at 18:48
9

Try this with bash:

cat k.txt <(echo) h.txt > new.txt
4
  • Did you read what the questioner said about cat in the question?
    – JdeBP
    Jun 23, 2015 at 17:11
  • I've updated my answer.
    – Cyrus
    Jun 23, 2015 at 17:12
  • Yup, that will work. Unless neither file ends with a newline. You might want to add another echo to fix that.
    – terdon
    Jun 23, 2015 at 17:20
  • Yes, if there is no newline in second file then a second <(echo) could be useful after h.txt.
    – Cyrus
    Jun 23, 2015 at 17:23
5

Using sed:

sed '/^/ r h.txt' k.txt

or better (thx @steeldriver)

sed '$a\' k.txt h.txt

Using ed:

(echo "0a"; cat k.txt; echo "."; echo "wq") | ed -s h.txt

and for the missing newline in k.txt:

(echo "0a"; cat k.txt; echo ""; echo "."; echo "wq") | ed -s h.txt

or if you need a separate output file:

(echo "0a"; cat k.txt; echo ""; echo "."; echo "w out.txt"; echo "q") | ed -s h.txt
3
  • Nice! How would you do it if neither file has a newline?
    – terdon
    Jun 23, 2015 at 17:43
  • The sed version will only work if k.txt has exactly one (non-terminated) line, I think? A better sed option from How to add a newline to the end of a file? might be sed '$a\' k.txt h.txt Jun 23, 2015 at 18:11
  • @steeldriver thx and updated =)
    – A.B.
    Jun 23, 2015 at 18:16
5

That's the job for paste:

paste -sd'\n' file1 file2
  • -s make paste concatenate all of the lines of each file in command line order.
  • -d'\n' make paste used newline as delimiter.
2
  • Though why they didn't add one more parameter to 'cat' for this purpose (inject newlines !) I do not know ...
    – MikeW
    Oct 4, 2016 at 15:57
  • Much nicer and works with any number of files. Aug 17, 2022 at 15:37
3

Realizing that the file1 might not contain the newline, why not simply add the newline by yourself ? (cat file1.txt; printf "\n"; cat file2.txt ) > out.txt. Alternative way to do this, would be printf "\n" | cat file1.txt - file2.txt

1
  • 5
    IMHO printf "\n" | cat file1.txt - file2.txt would be more idiomatic (and spares the life of one poor cat) Jun 23, 2015 at 18:07
1

Nobody has mentioned python yet. Here it is:

#!/usr/bin/env python2
with open('k.txt') as fk, open('h.txt') as fh, open('out.txt', 'a') as fo:
    for line in fk:
        fo.write(line)
    fo.write('\n')
    for line in fh:
        fo.write(line)

Here after reading the f.txt file we have inserted a newline manually in the out.txt file (fo.write('\n')) and then again append the content the k.txt file to the out.txt file. Finally out.txt will contain the desired output.

2
  • He python =) +1
    – A.B.
    Jun 23, 2015 at 18:35
  • @A.B. Should i call you ABed or ABerl may be ;)
    – heemayl
    Jun 23, 2015 at 18:39
0

sed '' h.txt k.txt > newFile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .