Add '|' to data file on particular position

I have below scenario where i am getting a flat file but with some bad data

incoming data format

id|name|ind|crncy
123|xxx|y|usd
234|yy|n|
456|a|y90.5|vvv|gbp ----bad data

need to cleanse the bad data above by adding a pipe '|' after 3rd column 'ind' if pipe '|' is not already there

desired output

id|name|ind|crncy
123|xxx|y|usd
234|yy|n|
456|a|y|90.5|vvv|gbp

Note: Indicator column value can be Y or N ,if that helps

Hi, try:

awk 'NR>1 && $3!~/^[yn]$/{sub(/./,"&|",$3)}1' FS=\| OFS=\| infile1
2 Likes