[SOLVED] Using sed to search and replace backwards
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Rep:
Using sed to search and replace backwards
Hi All,
I'm trying to use sed to search and replace backwards. The problem is that I have a shell script that is required to put commas into big numbers. For example
9999999 as 9,999,999
I've tried a few things, but none seem to work:
Code:
$ echo 9999999 | sed -e 's/\([0-9]\{3\}\)/,\1/g'
,999,9999
$ echo 9999999 | sed -e 's/\([0-9]\{3\}\)$/\1,/g' -e 's/\([0-9]\{3\}\)/\1,/g'
999,999,9,
$ echo 9999999 | sed -e 's/\([0-9]\{3\}\)$/\1,/g' -e 's/\([0-9]\{3\}\)/,\1/g'
,999,9999,
$ echo 9999999 | sed -e 's/\([0-9]\{3\}\)$/,\1/g' -e 's/\([0-9]\{3\}\)/,\1/g'
,9999,,999
$ echo 9999999 | sed -e 's/\([0-9]\{3\}\)$/,\1/g' -e 's/\([0-9]\{3\}\)/,\1/g'
It would be much easier if I could search backwards! For example Bash parameter substitution style:
Code:
$ echo 9999999 | sed -e 's%\([0-9]\{3\}\)%,\1%g'
Or may be someone has a better way to do this altogether...
It should depend from your current locale, in particular from the LC_NUMERIC variable. For example, the POSIX locale does not have a thousands separator, while en_US or en_GB should work. What is the output of the locale command (without arguments it should list your current setup)?
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Original Poster
Rep:
I've marked this as 'solved' although I would still be interested to see how I can make the printf solution work and how to actually get sed to search backwards (if this is possible).
thank you for reporting that. I also tried on a Solaris machine and I cannot make it to work in any way (even with en_US locale set for all applications). I suspect the "%'d" format has been introduced in a C language specification which has not been adopted by all systems. Maybe, better to stick with the regular expression solution!
It's not very satisfactory though, because I can't see a good reason why one shouldn't be able to search/replace in reverse order with sed.
Sorry if that makes me sound ignorant - I don't mean to be!
tac doesn't exist in my environment (I'm using Solaris), but I guess I could do the same thing with perl...
Using /bin/printf
Code:
$ LC_NUMERIC=en_GB /bin/printf "%'d\n" 9999999
'd
May be there's something specific to Solaris that I'm missing here? It's good to know that it works in (I assume this is where you're testing it) Linux!
May be there's something specific to Solaris that I'm missing here?
I just checked on the GNU coreutils and GNU C library documentation and it clearly states the format specifier for thousands separator is a GNU specific extension. So there is no way to let it work with native solaris utilities. Here is the relevant link.
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Original Poster
Rep:
It's a pity.
(Generally) you don't want to actually update the value in your variable - as you might want to go on an use it for mathematical purposes. Most tools will interpret the value as a string if it contains characters other than [0-9].
Being able to printf the thousands separator would be the perfect solution... May be I need to ask my SA to install GNU printf...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.