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
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: http://paste.ubuntu.com/606010/
– Peter.O
May 11 '11 at 05:42
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
Try:
sed 's/\(.*\)/\1 \1/' data.txt
\0 to match the entire expression, so s/.*/\0 \0/
– Michael Mrozek
May 10 '11 at 17:20
&.
– Gilles 'SO- stop being evil'
May 10 '11 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: http://paste.ubuntu.com/606010/
– Peter.O
May 11 '11 at 05:38
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...
– Peter.O
May 11 '11 at 06: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
sed/awkto manipulate a text file – Michael Mrozek May 10 '11 at 18:32sed/awklanguage to achieve the goal. I never would complain if someone asks a question aboutsed/awkon unix.stackexchange. But I really don't understand why it has to be migrated. I could also ask why this is aunixquestion. I thinksedandawkare available for other systems, too. – bmk May 10 '11 at 18:41sedandawkcould be called programming languages, but you're literally just replacing a line with itself twice -- calling that programming is like sayingexportis part of the bash programming language – Michael Mrozek May 10 '11 at 18:46