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
  • 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
  • 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
12

Here is a simple solution using paste:

paste -d ' ' file.txt file.txt
| improve this answer | |
  • 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: paste.ubuntu.com/606010 – Peter.O May 11 '11 at 5: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 7:16
11

Try:

sed 's/\(.*\)/\1 \1/' data.txt
| improve this answer | |
  • 8
    You can also use \0 to match the entire expression, so s/.*/\0 \0/ – Michael Mrozek May 10 '11 at 17:20
  • 5
    or & (also entire matched part) – ninjalj May 10 '11 at 18:40
  • 2
    @Michael: You can? Oh, it's a GNU extension. Not in POSIX or OpenBSD. Rather pointless extension since there's &. – Gilles 'SO- stop being evil' 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
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'
| improve this answer | |
  • 1
    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
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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy