Is there better way to escape pipe(|) from the pipe separated file?
I have pipe separated file, I tried running the awk command, it is working fine only for the records who doesn't have escaped double quotes. because it is considering field separator as double quotes.
Input file:
"first | last | name" |" john | white "| age | 52
school |" ABC | USA "| year | 2016
Home | Road | year\" | 1989\"
company |" Pvt | ltd "| joining | 2019
Code:
awk '
BEGIN { FS=OFS="\"" }
{ for (i=2;i<=NF;i+=2)
gsub(/\|/,"\\|",$i)
print
}
' testfile.txt
Output I am getting:
"first \| last \| name" |" john \| white "| age | 52
school |" ABC \| USA "| year | 2016
Home | Road | year\" \| 1989\"
company |" Pvt \| ltd "| joining | 2019
Expecting output :
"first \| last \| name" |" john \| white "| age | 52
school |" ABC \| USA "| year | 2016
Home | Road | year\" | 1989\"
company |" Pvt \| ltd "| joining | 2019
In 3rd Row, it is escaping pipe after year, but it is wrong as that double quote is part of 3rd column. Can I work on particular column to escape pipe if it belongs to same column