one more issue last- sed

hi

i have following sed command

this replaces "** in filename1 with octal value 007 filename2

when i put it in script it wont work but it works from command line

my OS is sun OS

---------- Post updated at 06:38 PM ---------- Previous update was at 06:14 PM ----------

i think i use bash shell so it wont work

what should i change to make it work in bash

---------- Post updated at 06:39 PM ---------- Previous update was at 06:38 PM ----------

i think i use bash shell so it wont work

what should i change to make it work in bash

I'm guessing that bash's built-in echo command isn't treating \007 the way you'd like. I personally find that echo varies so widely between systems/shells that I avoid using it at all possible costs. I also do not advocate breaking a sed string to "insert" shell generated text -- hard to maintain in my humble opnion.

If I were doing it, I'd tackle it this way as it should work regardless of shell:

awk 'BEGIN { r=sprintf( "%c", 7 ); } {gsub( "\"\\*\\*", r ); print}' filename >filename2

This replaces the "** with the bell character (7) and then prints the record.

Also, I'm sure someone will chime in that your use of cat is unnecessary as sed can read the input file directly, so I'll point it out:

sed 's/this/that/g' filename >filename2

No need to cat the file and pipe to sed -- not efficient.

the above statment doesn't work

it creates a zero byte file

:frowning:

it a little bit urgent

can someone help

What O/S are you using? If you are on Sun OS you should use nawk rather than awk.

Ive tested this both on a SUSE Linux and FreeBSD system and it works as expected; sorry it's not working for you.

i use sun os

so should i replace awk with nawk?

Yes -- I hope that will do the trick. I don't use Sun any more, so I cannot test there, but I remember awk on the Sun as being very limited.

it didn't work

This is a real hack, but I cannot think of anything else to try without having a Sun to run the test on myself. Try this, but see the note below before you change your script:

awk '{gsub( "\"\\*\\*", "^G" ); print}' filename >filename2

The ^G is a control character entered using an editor, NOT the two characters ^ and G as appear here. Using vi to edit the script, and while in insert mode, type CTL-V and CTL-G to insert a control-g character into the file. You should see ^G reprented by vi. I'm not a fan of putting control characters into a script like this, but if it's the only way then one does what works.

If this doesn't work, then I'm out of suggestions. Hoping for the best, and sorry if you're left with it broken.

i just added -e in echo option in which helped to solve the problem

You have a space there between single quote and backtick. That causes error -- sed complains about unterminated s command. There is no need for echo or escaping quotes or comma.
Try this:

sed  's/","\*\*/\x07/' filename > filename2

And check with 'od':

$ echo '"blah","**ddsa","das**","dad"' | sed  's/","\*\*/\x07/'
"blahddsa","das**","dad"
$ echo '"blah","**ddsa","das**","dad"' | sed  's/","\*\*/\x07/'  | od -c
0000000   "   b   l   a   h  \a   d   d   s   a   "   ,   "   d   a   s
0000020   *   *   "   ,   "   d   a   d   "  \n
0000032
$ echo '"blah","**ddsa","das**","dad"' | sed  's/","\*\*/\x07/'  | od -x
0000000 6222 616c 0768 6464 6173 2c22 6422 7361
0000020 2a2a 2c22 6422 6461 0a22
0000032

You can insert a control character with crtl-v on the command line, no need to save it and use vi:

sed  's/","\*\*/^G/' filename > filename2

just type ctrl-v ctrl-g when typing the command.

1 Like

Nice catch -- I completely missed the space.

Thanks -- I read the man page and found no reference to being able to use \xXX. I tried \0xXX which didn't work and am glad to be able to take something new away from this thread.

True. I omitted that as the OP indicated it wasn't working from a script.