Search code examples
regexfileunixpipedelimited

How to remove all non-numeric characters from the value of nth column of a pipe delimited files in UNIX


I have file in UNIX with many records having pipe delimiter. I want to remove non alpha numeric and non special character from the value of column number 20 which contains phone number and will do right trim 10 digit.


Solution

  • awk -F\| '{ OFS="|";gsub(/[[:alpha:]]|[[:punct:]]/,"",$4);$0=substr($4,(length($4)-10),length($4)) }1' file
    

    Set the file delimiter to | and then use gsub to substitute any alpha character or punctuation to empty characters. Print lines.