Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is in file.txt:


Im looking for ways to make it become:

redcar redcar
bluecar bluecar
greencar greencar

I've tried many ways using sed with no luck

share|improve this question

migrated from stackoverflow.com May 10 '11 at 17:37

This question came from our site for professional and enthusiast programmers.

Actually I don't understand why this question was migrated from stackoverflow. That's definitely a programming question. –  bmk May 10 '11 at 18:27
@bmk How is this a programming question? It's about using sed/awk to manipulate a text file –  Michael Mrozek May 10 '11 at 18:32
@Michael Mrozek: Yes - but what you have to do is to program using the sed/awk language to achieve the goal. I never would complain if someone asks a question about sed/awk on unix.stackexchange. But I really don't understand why it has to be migrated. I could also ask why this is a unix question. I think sed and awk are available for other systems, too. –  bmk May 10 '11 at 18:41
@bmk I guess technically sed and awk could be called programming languages, but you're literally just replacing a line with itself twice -- calling that programming is like saying export is part of the bash programming language –  Michael Mrozek May 10 '11 at 18:46
@bmk I talked with some SO mods; we're going to leave sed/awk-type questions alone in the future. Thanks for bringing it up –  Michael Mrozek May 10 '11 at 20:32

4 Answers 4


sed 's/\(.*\)/\1 \1/' data.txt
share|improve this answer
You can also use \0 to match the entire expression, so s/.*/\0 \0/ –  Michael Mrozek May 10 '11 at 17:20
or & (also entire matched part) –  ninjalj May 10 '11 at 18:40
@Michael: You can? Oh, it's a GNU extension. Not in POSIX or OpenBSD. Rather pointless extension since there's &. –  Gilles May 10 '11 at 21:55
General info.. For larger file sizes, this \1 \1 method is up to 30 times slower than Hai Vu's paste method.. and it is also slower than & &... Here is a link to my test script: paste.ubuntu.com/606010 –  Peter.O May 11 '11 at 5:38

Here is a simple solution using paste:

paste -d ' ' file.txt file.txt
share|improve this answer
Interesitng, and surprising!... Surprising because it is notably faster than sed -e "s/\(.*\)/\1 \1/" and sed -e "s/.*/& &/" for anything but the smallest files.. I did some time tests, and although is takes approximately the same time for a file with 1 line, paste becomes relatively much faster as the number of lines increases.. upt to a whopping 28 times faster with 1,000,000 lines... Here is a lin to the test snippet: paste.ubuntu.com/606010 –  Peter.O May 11 '11 at 5:42
Thanks @fred fro the performance test. –  Hai Vu May 11 '11 at 6:59
@fred That's not so surprising. A specialized tool such as paste has optimized code for its particular task, unlike sed or awk which are interpreters. The penalty you pay is that a specialized tool can only do one job. –  Gilles May 11 '11 at 7:16

There Is More Than One Way To Do It:

Substitute two times the full sentence:

sed 's/.*/& &/'

Copy to hold space, append hold space to pattern space, fix newline:

sed 'h;G;s/\n/ /'

awk, concatenate whole sentence using field separator:

awk '$0=$0FS$0'
share|improve this answer
I've done some time tests: paste.ubuntu.com/606010 ... For large files, sed -e "h;G;s/\n/ /" is marginally faster than sed -e "s/.*/& &/", which is marginally faster than awk '$0=$0FS$0', but they are all approx 10 times slower than paste -d ' ' file file (as suggested by Hai Vu .. I am quite surprised, but that's what the times show... –  Peter.O May 11 '11 at 6:34

I would do it in perl but since you put awk, I will give you awk code

awk '{print $0,$0;}' file.txt

Edit: remove my useless cat

share|improve this answer
UUOC (partmaps.org/era/unix/award.html) –  dogbane May 10 '11 at 17:21
@dogbane .. lol. I am guilty of UUOC. –  Aater Suleman May 10 '11 at 17:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.