0

File has control Z( ^Z) characters in them. I tried sed 's/^Z//g' file_name but not working. Even tried perl script but not removing them. Please let me know if there is way to remove this character.

3

The control character produced by Ctrl+Z is \032 in octal. This can be deleted by tr:

tr -d '\032' <file >newfile

This deletes the characters anywhere in the file and creates a new file called newfile with the modified contents.


Your sed command does not work as the expression ^Z would match a Z character at the start of the line. The ^ anchors the rest of the expression at the start of the line.

  • Note on calculating code for control characters. A is 1, B is 2 … Z is 26. – ctrl-alt-delor Jul 4 '18 at 8:54
0

You can match Ctrl+Z using \d plus the decimal code of the character (in this case 26).

sed 's/\d26//g' < file > newfile

A "posix" alternative (in bash) could be something like:

sed $( echo 's/@//g' | tr '@' '\032' ) < file > newfile

The tr will replace '@' in regex with the carachter Ctrl+Z (which code is 32 in octal).

  • Only with GNU sed though. – Kusalananda Jul 4 '18 at 8:42
  • @Kusalananda, are you sure? sed --posix 's/\d26//g' works. – andcoz Jul 4 '18 at 8:44
  • With any other sed implementation, s/\d26//g would delete all instances of the literal string d26. Using --posix does not disable extensions in GNU sed's regular expression library. – Kusalananda Jul 4 '18 at 8:47
  • Fascinating. @Kusalananda, you are right (as usual). – andcoz Jul 4 '18 at 10:04
  • The second solution use both sed and tr when tr is good enougth by itself: tr -d '\032' <file >newfile. – Isaac Jul 4 '18 at 16:08
-1
$ sed 's/\^Z//g' filename

NOTE: Special characters should be escaped with '\'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.