Reputation: 18702
I want to add a specific string at the beginning of each line in a file. So, if I have the below two lines in someFile.txt and want to add a date string 03/06/2012 with pipe-
Hello|there|john
Hello|there|joel
I would have-
03/06/2012|Hello|there|john
03/06/2012|Hello|there|joel
What would be the most efficient way to achieve this?
Upvotes: 20
Views: 45385
Reputation: 3451
Perl solution:
perl -ne 'print "03/06/2012|$_"' input.txt > output.txt
Just for fun, I benchmarked 10 runs in /tmp:
Rate awk perl_5.6.1 sed perl_5.22 perl_5.20
awk 2.08/s -- -10% -10% -26% -32%
perl_5.6.1 2.32/s 11% -- -0% -17% -24%
sed 2.33/s 12% 0% -- -17% -24%
perl_5.20 3.06/s 47% 32% 31% 9% --
Tested using a 1.3M line input file created here:
perl -le 'while (1){exit if ++$n > 1300000; print $n}' > input.txt
Upvotes: 1
Reputation: 188004
$ awk '{print "03/06/2012|" $0;}' input.txt > output.txt
Takes about 0.8 seconds for a file with 1.3M lines on some average 2010 hardware.
Upvotes: 32