Help with bash shell script

Hi,

I have a file in which records contains non ascii characters. The records are comma delimited and quoted.

The non ascii characters are found in a particular column.

Example records

"YY","AK000021","�","IO","PP"
"Y1","AK000022","�","PO","PP"
"Y2","AK000022","�","PO","PP"

I need to replace these special characters with null

After replacing it should look like this

"YY","AK000021","","IO","PP"
"Y1","AK000022","","PO","PP"
"Y2","AK000022","","PO","PP"

Please help me to write unix command to replace as above.

Thanks
Aks

Do you mean "replace it with null" as in replace it with ASCII null, or "replace it with null" as in delete it?

Either's possible with tr.

# Delete
tr -d '[\177-\377]' < input > output
# Replace with ascii null
tr -s '[\177-\377]' '\000' < input > output
1 Like

Thanks!! it is removing non ascii characters but, it it is removing pound(�) character which is there in other filed of the file. I want to retain pound character,

aim is to remove remove non ascii character only to that filed. Or in other way if highlighted filed has character other than P or Q then it should replace with empty value.

e.g
Sample File
------------
"Y3","I got � 20","P","IO","PP"
"Y5","I got � 20","Q","IO","PP"
"YY","I got � 20","<88>","IO","PP"
"Y1","AK000022","ã","PO","PP"
"Y2","AK000022","¿","PO","PP"

Expected file
------------
"Y3","I got � 20","P","IO","PP"
"Y5","I got � 20","Q","IO","PP"
"YY","I got � 20","","IO","PP"
"Y1","AK000022","","PO","PP"
"Y2","AK000022","","PO","PP"

Thanks
Aks