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.
-
Have a look at this similar question, the ascii code for "^Z" is "\032"kaliko– kaliko2018-07-04 09:17:09 +00:00Commented Jul 4, 2018 at 9:17
3 Answers
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– ctrl-alt-delor2018-07-04 08:54:44 +00:00Commented Jul 4, 2018 at 8:54
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
sedthough.2018-07-04 08:42:56 +00:00Commented Jul 4, 2018 at 8:42 -
@Kusalananda, are you sure?
sed --posix 's/\d26//g'works.andcoz– andcoz2018-07-04 08:44:54 +00:00Commented Jul 4, 2018 at 8:44 -
With any other
sedimplementation,s/\d26//gwould delete all instances of the literal stringd26. Using--posixdoes not disable extensions in GNUsed's regular expression library.2018-07-04 08:47:17 +00:00Commented Jul 4, 2018 at 8:47 -
Fascinating. @Kusalananda, you are right (as usual).andcoz– andcoz2018-07-04 10:04:49 +00:00Commented 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.user232326– user2323262018-07-04 16:08:58 +00:00Commented Jul 4, 2018 at 16:08