Karthick Karthick - 3 months ago 8
Ruby Question

ruby delete hidden characters from entire file

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

\033[H\033[2J\033


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

^[[H^[[2J^[[H^[[2J


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.

Answer

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
end

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]/, '')
Comments