14

I copied this line from /proc/some_proc_id/cmdline in my ubuntu machine,

java^@-jar^@/usr/lib/selenium/selenium-server-standalone.jar^@-port^@4444^@-trustAllSSLCertificates^@

Somehow, the space characters are represented by ^@ in vi. I tried to replace them with space characters using the command,

:%s#^@# #g

But it says, pattern not found ^@.

How can one replace special characters particularly those that start with carat symbol?

  • What is the terminal type you are using? – Ed Heal Jul 19 '15 at 14:09
  • at times, TERM and many times, it is screen – user93868 Jul 19 '15 at 14:11
  • What is the terminal program you are using and is the TERM environment variable set correctly? – Ed Heal Jul 19 '15 at 14:16
15

Somehow, the space characters are represented by ^@ in vi.

It's not vi that did that. Although you type command lines in shells with spaces between the arguments, command lines are actually discrete sequences of strings internally, not one long space-separated string. The shell separated the command line into individual argument strings before the command was launched.

In C, strings are terminated with NUL characters and those are shown as ^@.

How can one replace special characters particularly those that start with carat symbol?

In order to type those characters, you must prefix them with Control-v for literal next character.

For example in this case: Control-v followed by Control-@.

The special character that introduces literal next characters is normally Control-v but it is actually configurable. Type stty -a to find out what it is set to. Look for lnext in the output.

  • the post is informative.. but the real solution for my question is escaping the carat symbol - :%s#^@##g. Since i copy, pasted from terminal, vi has pasted them as original characters... – user93868 Jul 19 '15 at 14:56
  • 1
    @MadhavanKumar You don't escape the caret symbol, because there is no caret symbol. It's really an actual control character. ^letter is just visual notation for that, no real caret involved. And yes, you have to escape the control character, the Ctrl-V is how you do that from the keyboard. That works most everywhere. FYI, the other answer contains an alternative, specific to vi's command line, to avoid having to type the control character at all. – Celada Jul 19 '15 at 15:01
  • @MadhavanKumar If your terminal displayed the NUL characters as the two-character string ^@ and you copy-pasted that, then use \^@ to represent that two-character string in vi. A lot of software uses backslash to mean “interpret the next character literally instead of using its special function”. – Gilles 'SO- stop being evil' Jul 19 '15 at 21:44
  • @Gilles, you are right.. that is what i tried to convey to Celada.... – user93868 Jul 20 '15 at 3:13
4

That symbol represents a NULL character, with ASCII value 000.

You could try:

:%s/\%x00/ /g

Alternative:

On a keyboard where the @ symbol is on top of the 2 key (thanks Celada) you can do:

  • %s/<CTRL-2>/ /g (on PC)

  • %s/<CTRL-SHIFT-2>/ /g (on Mac)

where <CTRL-2> means first press down the CTRL on PC, keeping it as pressed down, hit 2, release CTRL.

and <CTRL-SHIFT-2> means first press down the control on Mac, keeping it as pressed down, press down shift on Mac, keeping it as pressed down, hit 2, release control and shift.

Finally, both of the two commands should result in %s/^@/ /g on screen. ^@ means a single character, not ^ followed by @, so you can't just type ^ and @ in a row in the above command.

This command replaces all the ^@ with space characters.

(Source)

  • When you say "PC", do you mean Windows? – Sparhawk Jul 19 '15 at 14:41
  • 1
    CTRL-2 and CTRL-SHIFT-2 only make sense on keyboard layouts where the @ is on top of the 2 key. – Celada Jul 19 '15 at 14:42
  • @Sparhawk: No, I mean a linux pc. :-) – erik Jul 19 '15 at 14:45
  • Why CTRL? I've never seen a system where CTRL is the key used to shift between the different meaning of a key. That's normally SHIFT. – terdon Jul 19 '15 at 18:53
  • It is strange (maybe a bug in vim?), but it works. It should usually be ctrl+shift+2 (on a us-keyboard). Ask it as a separate question or ask the vim developers. Anyhow Celada’s answer is more general for control-characters. – erik Jul 19 '15 at 19:01
0

sed 's/\r//' input > output

enter link description here

Your Answer

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