Remove ^@ Characters in a Unix File

谁都会走 提交于 2019-12-10 14:23:52

问题


I have a question about removing invisible characters which can be only be seen when we try to view the file using "vi" command. We have a file that's being generated by Datastage Application (Source is a DB2 Table -> Target is a .txt File). File has data with different data types. I'm having an issue with just 3 columns which have their datatypes defined as CHAR.

If you open the the file in a Textpad you'd see spaces. But when you view the same file on Unix via vi command, we see ^@ characters in blue color. My file is a delimiter file with the delimiter as ^@^ (I know it's kinda sounds weird) .

I have tried:

  1. tr -d [:cntrl:] <Filename >NewFileName — Still no luck — [Delimiters are removed but the spaces remain]
  2. tr -s "^@" <Filename >NewFilename — Still no luck — I see file reduce in file size but the invisible characters still stay.
  3. Tried changing the delimiter — but still see the same invisible characters.
  4. Used sed "s/^@/g/" (and other sed commands) <Filename — still no luck.

Any suggestions are really appreciated. I have researched the posts on this website but I couldn't find one. If it's a simple please excuse me and share your thoughts.


回答1:


In vi, NUL characters are represented as ^@. To get rid of them:

tr

Using tr, you should be able to remove the NUL characters as follows:

tr -d '\000' < file-name > new-file-name



回答2:


open the file with vim and then type ':' without the single quote and paste this:

%s/control + 2//g (on regular PCs)

%s/control + shift + 2 //g (on Mac PCs)

of course, replace with keys from your keyboard



来源:https://stackoverflow.com/questions/31846045/remove-characters-in-a-unix-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!