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
Here is a simple solution using paste:
paste -d ' ' file.txt file.txt
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
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.
Commented
May 11, 2011 at 7:16
Try:
sed 's/\(.*\)/\1 \1/' data.txt
\0
to match the entire expression, so s/.*/\0 \0/
Commented
May 10, 2011 at 17:20
&
.
Commented
May 10, 2011 at 21:55
\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
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'
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...
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
sed
/awk
to manipulate a text file