Karthick Karthick - 8 months ago 32
Ruby Question

ruby delete hidden characters from entire file

I have text file which contains the below hidden characters which I got with sed.


When I open file with vi and seeing the above code as below,


Due to this hidden characters am facing some issue while processing file. Is there any to get rid of this hidden characters in entire file before processing it.


If the file size is not too big, you can read the whole file contents in, and then remove all the escaped sequences.

content = File.read('your_input_file_path')
content.gsub!(/\033\[(?:H|2J)/, '')
content.split(/\r?\n/).each do |line|
  # process line

You can generalize the Regex used according to the escaped sequence pattern. In you example, it seems it's \033[ followed by an optional digit, and then a letter. Which can be updated as:

content.gsub!(/\033\[\d?[A-Z]/, '')