Sed copy first string and prepend it to line

56 Views Asked by At

I have a file:

Text1 text2 50
Text3 text4 60

I would like to make the following using sed command:

Text1 Text1 text2 50
Text3 Text3 text4 60

I need to copy the first string and add it to the line.

1

There are 1 best solutions below

0
stevesliva On BEST ANSWER
sed 's/[^ ]\+ /&&/' input-file

Assumes a space as separator, and not a tab. & is shorthand for "entire match" when used on replacement side of s/// substitution

Note that prior answer s/.* /&&/ above only works for 2-word inputs. It's too greedy. So we're matching one or more non-space characters followed by a space.