0

I have a file with following content:

$ cat -n w
     1  Smith   John    12345   jsmith
     2  Jones   Robert  23456   rjones
     3  Green   Ralph   34567   rgreen
     4  Brown   Fred    45678   fbrown
     5  Orange  Mabel   56789   morang

I want to get each line and then append at the end of that line the same text of that line.

Example I want this output:

$ cat -n w
     1  Smith   John    12345   jsmith  Smith   John    12345   jsmith
     2  Jones   Robert  23456   rjones  Jones   Robert  23456   rjones
     3  Green   Ralph   34567   rgreen  Green   Ralph   34567   rgreen
     4  Brown   Fred    45678   fbrown  Brown   Fred    45678   fbrown
     5  Orange  Mabel   56789   morange Orange  Mabel   56789   morange

I looked into sed command with backreferencing however I can't get it to work

$ sed 's/(.*)/\t\1/' wider
sed: -e expression #1, char 12: invalid reference \1 on `s' command's RHS
  • 2
    paste infile infile – don_crissti Feb 24 '16 at 15:23
  • @don_crissti, sorry I can't upvote this comment more than once. Sweet and to the point. – vonbrand Feb 24 '16 at 16:56
2

sed use BRE by default, you need to escape \(...\) to capture the matching:

LC_ALL=C sed 's/\(.*\)/\1\t\1/' <file

or using -E to enable ERE (which available in GNU sed and BSD sed, and will become standard soon), so you must not escape (...):

LC_ALL=C sed -E 's/(.*)/\1\t\1/' <file

Setting locale to C help you match invalid unicode characters.

But, it's better job for paste:

paste file file

or awk:

awk '{printf "%s\t%s\n", $0, $0}' <file
1

Try this:

sed 's/\(.*\)/\1\t\1/' <filename>
  • why escape ( ) brackets? – Saad A Feb 24 '16 at 15:25
0

if you don't have anything against using awk, here is a way to do it :

awk '{print $0"\t"$0}'

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.