Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search and replace '\274' in vi? 1

Status
Not open for further replies.

michael3

Programmer
Aug 8, 2001
26
US
Hi,

There is a special character '\274' in my text file, how can I find and replace it in vi editor or use sed?

In vi editor, this character is shown as '\274', a single character. When I use 'hd' to read the file, '\274' is 0xbc. But when I use 'cat' to read this file, '\274' shows as an equal sign ('='). This character is generated by "hold alt- key, and type 171". On screen, it shows as ½. Then save it into oracle, ...

/home/ops/oper/DBL/dly> hd *.out |grep bc
00000450 44 20 31 bc 20 41 4e 44 20 32 bc 2e 01 32 30 30 D 1. AND 2...200
00000a40 01 54 41 4b 45 20 32 bc 01 32 30 30 32 31 30 32 .TAKE 2..2002102
00000bc0 30 01 01 30 01 01 01 30 01 01 01 01 01 30 01 01 0..0...0.....0..
00001bc0 34 35 32 38 32 01 30 01 30 01 01 4e 01 30 01 30 45282.0.0..N.0.0

Thanks,
Michael

 
You could use awk

awk '{gsub(/\274/,"=")}' input-file > output-file

If anyone has a vi solution, I would be interested to see it. CaKiwi
 
normally in vi, to globally replace a string for another you would, while in command mode...

:s/stringtoreplace/replacementstring/g

however, the "\" in your string may, or may not, cause the substitution to work improperly.
You may try
:s/\\274//g
 
alt-171 seems to be 275 on my system for some reason (but shows up as the 1/2 symbol)

to find it you can just do
Code:
/<alt>171</alt>
where <alt> is where you start holding the alt key down and </alt> is where you let it go.

if that doesn't work you can do
Code:
/<ctrl>v</ctrl><alt>171</alt>

to substitute type
Code:
:s/<ctrl>v</ctrl><alt>171</alt>/substitute text/

to replace all occurances on that line put a 'g' at the end after the final /

to replace all occurances globally in the file type
Code:
:g/<ctrl>v</ctrl><alt>171</alt>/s//substitute text/g

i can get the alt-171 to work when i telnet from my PC to my unix box ... however i can't get the alt keys to work in my GUI because alt-1 is 'End' and alt-7 is 'Home' in my telnet window :)
 
Thanks, ALL. I have tried all solutions you provided, but unable to get the result.

Cakiwi, my sys doesn't like awk, instead I used gawk, but got a syntax err. Following are the screen print:
/home/ops/oper/DBL/dly> gawk '{gsub(/\274/,&quot;ZZZ&quot;)}' tmp >outfile
/home/ops/oper/DBL/dly> ls -l outfile
-rw-r--r-- 1 oper ops 0 Oct 23 10:11 outfile
/home/ops/oper/DBL/dly> awk '{gsub(/\274/,&quot;ZZZ&quot;)}' tmp >outfile
awk: syntax error near line 1
awk: illegal statement near line 1

pavNell, :s/\\274//g doesn't work since I can't search the single character '\274' in my system.


jad, when I used your solution, I was unable to type-in the expresion. :g/<ctrl>v</ctrl><alt>171</alt>/s//XXX/g
the error says &quot;no previous regular expresion&quot;.

Again, thanks a lot for help.

Michael
 
Sorry, I forgot the print statement.

gawk '{gsub(/\274/,&quot;ZZZ&quot;);print}' tmp >outfile

What system are you on? CaKiwi
 
Cakiwi,

I use DYNIX/ptx 4.0 V4.4.7 i386. Thanks,

M.
 
:s/AH/PT/g

in vi will substitute all 'AH's on the current line with 'PT's
<ctrl>v</ctrl> was meant to represent (hold down control key on keyboard)(press letter v)(release control key on keyboard)
similarly <alt>171</alt> was meant to represent (hold down alt key on keyboard)(press number 1 on keypad)(press number 7 on keypad)(press number 1 on keypad)(release alt key on keyboard)

ctrl-v is the code for inserting the following character as a literal ... ctrl-m for instance is normally a new line, so searching for/replacing ctrl-m's (in DOS documents for instance) is hard unless you use the ctrl-v before hand.

i'll now leave a clean example. i am now assuming that your funny character is the text string 'AH' and the text you want to substitute is 'PT'
/AH
will find all occurances of AH
:s/AH/PT/
will substitute the first occurance of AH for PT on the current line
:s/AH/PT/g
will substitute all occurances of AH for PT on the current line
:g/AH/s//PT/
will substitute the first occurance of AH on every line in the document for PT
:g/AH/s//PT/g
will substitute every occurance on every line of AH for PT

hope any of this helps someone :)
 
perl can do this such as

perl -pe 's/\xbc/=/' input > output

so in vi you can do

:%!perl -pe &quot;s/\xbc/=/g&quot; Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Try using tr (translates characters in UNIX) on the command line or in a shell script.
This example changes every octal \274 to X:

tr &quot;\274&quot; &quot;X&quot; < oldfile > newfile

In another example, tr can change lower case characters to upper case characters, etc:
tr &quot;abcde&quot; &quot;ABCDE&quot; < oldfile > newfile
if oldfile looks like:
apple pie
banana bread
chocolate cake
then newfile will look like:
ApplE piE
BAnAnA BrEAD
ChoColAtE CAkE

Hope this helps!
- ElenaT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top