Copy previous line to current line from text file

Using awk:

awk 'prev{ print prev ";" $0 }
     { prev = $0 }
     END { if (NR) print prev ";" }'

which with your input, gives

A;B
B;C
C;

Quick'n'dirty way (involves reading the file twice):

$ tail -n+2 file | paste -d';' file -
A;B
B;C
C;

$ sed 'x;G;s_\n_;_;1d;${p;x;s_$_;_;}' file
A;B
B;C
C;

What that sed expression is doing:

  • x: save the incoming line in hold space, and retrieve the previous one
  • G: append the new line (from hold space) to the old one
  • s_\n_;_: replace line-break with a ;.
  • 1d: if this is the first line, delete it (don't print it) and advance to next
  • ${...;}: if this is the last line...
  • p: first print the joined pair
  • x: retrieve the final line
  • s_$_;_: append final ;