-2

I have file test_output_archive.svd with some Dollar signs ($). I have generated a shell script Replace_dollar_pound.sh with cat command to replace dollar symbol with Pound (£) script is working on Solaris 10, Dollar getting replaced as expected. In Solaris 11, but the dollar is not getting replaced, not throwing any errors

Content of test_output_archive.svd : Line Rental $9 OOB Only for $10

Replace Dollar symbol with Pound at step1, Remove junk charactes at step 2, Remove temp file at step3, Rename at step4**

Content of Replace_dollar_pound.sh : cat test_output_archive.svd | tr "\044" "\243" > temp_archive.svd cat temp_archive.svd | tr -d "\302" > test_output_archive2.svd rm temp_archive.svd mv test_output_archive2.svd test_output_archive.svd

Any Modifications to be done as OS is updated to Solaris 11 ??

  • 4
    Hi and welcome to the site! When posting questions, please make sure to post the command that is giving you errors and the exact error message received. – terdon Jun 20 '14 at 12:40
  • 1
    Which sed is being used — /usr/ucb/sed, /usr/bin/sed or /usr/xpg4/bin/sed? If you don't know, edit your question to add the output of echo "$PATH". What are your locale settings (output of locale)? What encoding do you want the resulting file to be in? – Gilles Jun 20 '14 at 13:55
  • 1
    Can you post (include in your question) the output of LC_ALL=C sed -n l < del.sh? And of locale charmap? Two possibilities I'm thinking of: there's a hidden CR character at the end of the line, or you're entering £ as its latin1 representation (0xA3) instead of its UTF8 representation (0xC2 0xA3) in a UTF8 locale. – Stéphane Chazelas Jun 20 '14 at 14:27
4

The $ has a special meaning in regular expressions, it means "the end of the record" (record==line in most cases). So, you need to escape it. Try this:

sed 's/\$/£/g' file > outfile
  • I was thinking something similar, but not escaping $ doesn't result in a "command garbled error" (whatever that is). +1 anyway, as it answers the question as well as it can. – Sparhawk Jun 20 '14 at 12:42
  • @Sparhawk indeed, it's a valid command, it just means something else. – terdon Jun 20 '14 at 12:48
  • batch@BATCH:[~/output]$ ./del.sh sed: command garbled: s/\$/£/g – sirajuddin mohammed Jun 20 '14 at 13:18
  • 1
    @sirajuddinmohammed 1) we have no idea what's in del.sh 2) Please don't add this kind of thing in the comments. Edit your question where you can format it correctly. – terdon Jun 20 '14 at 13:25
  • Contents of del.sh : #!/bin/sh #!/bin/expect #!/bin/bash sed 's/\$/£/g' Dollar.txt – sirajuddin mohammed Jun 20 '14 at 13:28
0

You can escape the special character ($) in the sed command.

$ cat test.txt
£ pound £
$ dollar $
£ pound £
$ dollar $

$ sed 's/\$/£/g' test.txt
£ pound £
£ dollar £
£ pound £
£ dollar £

Or alternatively

$ sed 's/[$]/£/g' test.txt
£ pound £
£ dollar £
£ pound £
£ dollar £
  • sed: command garbled: s/\$/£/g – sirajuddin mohammed Jun 20 '14 at 13:22
  • try using "[$]" – sudocode Jun 20 '14 at 13:32
  • sed: command garbled: s/"[$]"/£/g sed: command garbled: s/[$]/£/g Please provide a way to display £ symbol. able to replace with some string. unable to replace with £ symbol. – sirajuddin mohammed Jun 20 '14 at 13:36
0

You can use sed to replace every occurrence of a string as described here:

sed -i 's/\£/\$/g' FILENAME

The -i option tells sed to operate on a file, s is the operator for replacing strings, is the string you need to replace, \$ is the string that replaces each $ character (\ escapes the signs as they are special characters) and g means replace every instances.

EDIT: the -i option doesn't work on Solaris, you should use this workaround.

  • 2
    £ is not a special character. Also, the OP wants it the other way around. – terdon Jun 20 '14 at 12:49
  • Anyway, £ needs to be escaped at least on my version of sed. Sorry, but where did the OP say that escaping is not a way to go for him? – psimon Jun 20 '14 at 12:52
  • Really? What version of sed is that? £ has no meaning in any regular expression language. The OP said no such thing (which is why I posted pretty much the same answer as you did :), I meant that he wants to replace $ with £ and not £ with $. – terdon Jun 20 '14 at 12:57
  • Oh, sorry, I misunderstood something. I use sed 4.2.1 on Debian Wheezy. And it's very strange as it only produces an error (sed: -e expression #1, chsed: -e expression char 8: unterminated s' command) on certain files. – psimon Jun 20 '14 at 13:05
  • And escaping the £ solves that? That's very strange. I'd love to see an example of such a file. If you have the time/inclination, come into chat and show me. – terdon Jun 20 '14 at 13:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.