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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed or vi search and replace of octal codes. 1

Status
Not open for further replies.

sysanalyst25

Technical User
Nov 6, 2002
5
0
0
US
A customer of mine exported a Sybase database in a .bcb format. In some of the .bcp files the character strings with trailing spaces had a \040 instead of a " ". Does anyone know how to search and replace an octal code? The following is an example.
NOTE: The "\040" in vi is one character.

2135|0|TRUE|1|0|NEWCKT:\040|0|0|0
2135|1|TRUE|1|0|SLOT:\040|0|0|0
2136|0|TRUE|1|0|DELCKT:\040|0|0|0
2136|1|TRUE|1|0|SLOT:\040|0|0|0
 
Hi,

in vi do

:%s/\\040/"/g

or you can do it on the shell prompt

sed -e s/\\040/&quot;/g <file*bcb> > newfile

Hope I've helped

Best regards

 
Hi again,

I was noting something strange in my advice so I boot up my pc in linux and tried wich in fact didn't result because I forgot to use the quotes,anyway heres how its done:

shell way...
[root@penantes root]# cat testes
2135|0|TRUE|1|0|NEWCKT:\040|0|0|0
2135|1|TRUE|1|0|SLOT:\040|0|0|0
2136|0|TRUE|1|0|DELCKT:\040|0|0|0
2136|1|TRUE|1|0|SLOT:\040|0|0|0
[root@penantes root]# sed -e s/'\\040'/'&quot;'/g testes
2135|0|TRUE|1|0|NEWCKT:&quot;|0|0|0
2135|1|TRUE|1|0|SLOT:&quot;|0|0|0
2136|0|TRUE|1|0|DELCKT:&quot;|0|0|0
2136|1|TRUE|1|0|SLOT:&quot;|0|0|0
[root@penantes root]#

just redirect the stdout to a file with &quot; > file&quot; at the end.

vi way..

just type

:%s/\\040/&quot;/g on the vi


At least it worked on Linux if there isn't a reply from you tomorrow morning I'll test it on solaris 2.6.

Best regards


 
sysanalyst25,

It's very strange that it should be shown as \040 in vi, because that *is* the octal code for the space character.

Antoher option would be to search and replace for something like :.| and replace it with :| (where the . matches any character). Naturally you would need to be careful that it didn't match any strings you didn't want to change! Annihilannic.
 
Don't know about the other suggestions but I successfully tested the following:

sed 's/\\040/ /g' <infile >newfile

Note that the middle of the command is &quot;slash space slash gee

 
I had already tried all of that. The problem is that vi doesn't recognize \040 as four individual characters, it is one character so the %s/\\040//g will not work. What it does is find all occurrences of the actual string 040 in the file and not the octal code. What Annihilannic said was the only thing that I was able to get to work, but I have several hundred files that I need to change so I need to write a script using sed and I would end up deleting stuff that I don't want deleted.

I know how to write the script and I know how to search and replace strings, but I don't know how to search and replace octal codes. If anyone is interested I can send you the file so you can see what I'm talking about.

Oh, if you are in vi and you move the cursor one character to the right from the : you end up on the last 0 in \040.

Thanks for all the help

Jeremiah
 
If you cat my test file it looks like this.
cat tmp.test
2135|0|TRUE|1|0|NEWCKT: |0|0|0
2135|1|TRUE|1|0|SLOT: |0|0|0

but in vi it looks like this.

2135|0|TRUE|1|0|NEWCKT:\040|0|0|0
2135|1|TRUE|1|0|SLOT:\040|0|0|0

I ran od on the test file and found that the octal representation is 240 and hex is a0. It turns out the character isn't a space. If you run man ascii it doesn't evaluate to any character.

od -x tmp.test
0000000 3231 3335 7c30 7c54 5255 457c 317c 307c
0000020 4e45 5743 4b54 3aa0 7c30 7c30 7c30 0a32
0000040 3133 357c 317c 5452 5545 7c31 7c30 7c53
0000060 4c4f 543a a07c 307c 307c 300a
0000074

od -b tmp.test
0000000 062 061 063 065 174 060 174 124 122 125 105 174 061 174 060 174
0000020 116 105 127 103 113 124 072 240 174 060 174 060 174 060 012 062
0000040 061 063 065 174 061 174 124 122 125 105 174 061 174 060 174 123
0000060 114 117 124 072 240 174 060 174 060 174 060 012
0000074

I think I'm close to figuring this out, but any help would be appreciated. Thanks

Jeremiah

 
Does this awk script do what you want?

awk '{gsub(/\240/,&quot; &quot;);print}' infile > outfile CaKiwi
 
sysanalyst25,

Thanks for the complete info. I've generated a binary exact copy of tmp.test, and the following seems to do the trick:

[tt]tr -d &quot;\240&quot; < tmp.test > outfile[/tt]

Why it's display \040 instead of \240 in your version of vi is a bit of a mystery... it's \240 when I try it. Annihilannic.
 
Thanks for everyone's help. I did end up figuring out how to do it with sed once I found out it was 240 not 040 it was easy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top