sed to work on 2nd field only

I have a requirement to replace "\" with "/" in only the 2nd field of the input file which has 2 fields. The field delimiter is "|"

Sample records from input file:

1\23|\tmp\user
mn\wer|\home\temp

Expected output:

1\23|/tmp/user
mn\wer|/home/temp

I used

sed 's/\\/\//g'

This would replace all "\" to "/". I want the replacement to happen only in the second field.

Any suggestions?

Does it have to be sed? awk would be very simple:

$ awk -F\| '{gsub(/\\/, "/",$2)}1' OFS=\| file
1\23|/tmp/user
mn\wer|/home/temp

With sed, try

$ sed 'h;s:.*|::;s:\\:/:g;x;s:|.*:|:;G;s:\n::' file
1\23|/tmp/user
mn\wer|/home/temp
1 Like

Thanks, it worked although the second option is a little tougher to understand!