10

I am unable to type (Control A) characters in a shell script. By using (Ctrl+V) and then (Ctrl+A). I am unable to do so. I am able to give (Ctrl+A) from cli but not in a shell script. I am typing the shell script in vi.

What I am trying to do is write a shell script containing the command sed 's/^A//g', i.e. trying to replace (Ctrl+A) with NULL.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Joy Jyoti
  • 211
  • 3
  • 12
  • not a direct answer but what about tr -s '001' 'X' ? (Ctrl-A is 001) – PW. Jul 04 '14 at 09:35
  • What i am trying to do is - sed 's/^A//g' i.e. trying to replace (Ctrl+A) with NULL. How should i put what you suggested in the above sed cmd? – Joy Jyoti Jul 04 '14 at 09:39
  • 1
    There must exists a spimplier/faster form, but at least this one work: sed -e 's/'$(echo -e "\0001")'/'$(echo -e "\000")'/g' (actually it will replace with NUL, if you mean empty, just remove the second echo) – PW. Jul 04 '14 at 11:45
  • 1
    which shell and editor ? – NeronLeVelu Jul 04 '14 at 11:58
  • @PW - if i need it to work for Control-M, sed -e 's/'$(echo -e "\1101")'//g' - does not work. It gives an error : Invalid back reference. – Joy Jyoti Jul 07 '14 at 10:38
  • @PW - while trying to replace (Control-A) are we echo'ing the decimal value of Ctrl-A or the Hexadecimal value? – Joy Jyoti Jul 07 '14 at 10:55

2 Answers2

8

@Joy, If you are using Vi/Vim, just make sure you are in Insert mode and then press (Ctrl+V) followed by (Ctrl+A) to get ^A typed. Sorry, I really don't see why this wouldn't work... Maybe your (Ctrl+V) keypress is caught by some other software layer?

jaybee
  • 925
  • 4
  • 11
  • I am in insert mode. (Ctrl+V) followed by (Ctrl+A) to get ^A typed just does not work from vi in my UAT env. It works fine outside vi editor (from command line) but does not work via vi editor. Approach as suggested by 'PW' has helped. – Joy Jyoti Jul 06 '14 at 11:43
0

This works for me:

cat file
This is a test ^A more data

sed 's/\^A//g' file
This is a test  more data
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • The replace will work. My question perhaps is wrong. I am trying to write a shell script where i want to write - sed's;^A;;g' I am unable to type the ^A in a shell file. I am trying (Ctrl+V) and then (Ctrl+A) to get ^A typed. I am getting the error - E29: No inserted text yet in vi – Joy Jyoti Jul 04 '14 at 11:29