2

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.

1
  • Have a look at this similar question, the ascii code for "^Z" is "\032" Commented Jul 4, 2018 at 9:17

3 Answers 3

5

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.

1
  • Note on calculating code for control characters. A is 1, B is 2 … Z is 26. Commented Jul 4, 2018 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).

6
  • Only with GNU sed though. Commented Jul 4, 2018 at 8:42
  • @Kusalananda, are you sure? sed --posix 's/\d26//g' works. Commented Jul 4, 2018 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. Commented Jul 4, 2018 at 8:47
  • Fascinating. @Kusalananda, you are right (as usual). Commented Jul 4, 2018 at 10:04
  • The second solution use both sed and tr when tr is good enougth by itself: tr -d '\032' <file >newfile. Commented Jul 4, 2018 at 16:08
-2
$ sed 's/\^Z//g' filename

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.