6

This is in file.txt:

redcar
bluecar
greencar

Im looking for ways to make it become:

redcar redcar
bluecar bluecar
greencar greencar

I've tried many ways using sed with no luck

  • 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
  • @Michael: OK - I have to admit that that's not a very complex programming task (but if you see the answer of ninjalj - it can also be done in a somewhat more complex way). But nevertheless I think the question is more programming than unix (btw.: why is it unix at all?). – bmk May 10 '11 at 18:58
  • @bmk Well, it's using *nix tools; what would you consider valid Unix questions if not this? – Michael Mrozek May 10 '11 at 19:01
  • 1
    @Michael Mrozek: I kind of agree with bmk. I think this shouldn't be migrated either if it appeared originally on unix or on stackoverflow, it's relevant for both, OTOH, if it appeared on programmers ... – ninjalj May 10 '11 at 19:13
  • 1
    @ninjalj Well, you're arguing with the wrong group -- tell that to the SO mods :). My decision is just "should this be closed on UL or left open" -- I think everyone agrees it's fine here – Michael Mrozek May 10 '11 at 19:15
  • @Michael: OK - you're right. The discussion is wrong here - it should be discussed on SO. And: No - the question should not be closed here. It's (as ninjalj said) also fine here. – bmk May 10 '11 at 19:44
  • 2
    @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 Answers4

12

Here is a simple solution using paste:

paste -d ' ' file.txt file.txt
Hai Vu
  • 1,201
  • 1
    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: http://paste.ubuntu.com/606010/ – Peter.O May 11 '11 at 05:42
  • 2
    @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 'SO- stop being evil' May 11 '11 at 07:16
11

Try:

sed 's/\(.*\)/\1 \1/' data.txt
7

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'
ninjalj
  • 1,427
  • 1
    I've done some time tests: http://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 06:34
4

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