Place , character after 3 digits from left to right in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Place , character after 3 digits from left to right in a string
# 8  
Old 05-14-2012
Place , character after 3 digits from left to right in a string

Hi Mirni,

Code:
echo 123456 | rev | sed 's/\([0-9][0-9][0-9]\)/\1,/g' | rev

result -
Code:
,123,456

but i don't need , comma from Begin it should be either Zero(0) or null (nothing).



Please help me ...

Thanks,
Krupa

Last edited by Franklin52; 05-14-2012 at 04:36 AM.. Reason: Please use code tags
# 9  
Old 05-14-2012
Hmmm... I'd add another command to sed:
Code:
echo 123456 | rev | sed -e 's/\(...\)/\1,/g' -e 's/,$//' | rev

Alternatively, you could do:
Code:
echo 123456 | rev | sed -e 's/\(...\)\(.\)/\1,\2/g' | rev

This User Gave Thanks to mirni For This Post:
# 10  
Old 05-14-2012
This is a very interesting problem. Perhaps the best (=simpliest) solution is the one from Scrutinizer and it is quite idle musing to try something in one tool (sed) which could be done a lot easier in another. Still, just to satisfy my curiosity:

Several posters here have suggested variants of the following algorithm: replace every three characters with these characters plus an appended comma. Reverse before and after this:

Code:
echo <number> | rev | sed -e 's/\(...\)/\1,/g' | rev

This may work but it is interesting to try to solve it in sed alone. It is possible to set up a loop appending the rightmost character to holdspace until the pattern space is empty. This would effectively do the same as the rev utility.

But there is a much simplier way to achieve our goal. Let us first apply our algorithm without using the rev utility. The outcome, depending on the length of the number, would be one of the following three forms:

Code:
1234   -> 123,4
12345  -> 123,45
123456 -> 123,456,

We can correct the first one by moving all the commata 2 places leftwards. The second variant can be corrected by moving the commata 1 place leftwards and the last line just has one superfluous comma in the end. Therefore:

Code:
echo 12345678901234 |\
sed 's/\(...\)/\1,/g
     /,.$/ {
          s/\(..\),/,\1/g
          }
     /,..$/ {
          s/\(.\),/,\1/g
          }
     s/,$//'

Which will do the trick in sed alone. My curiosity is satisfied indeed.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 11  
Old 05-14-2012
Hi.

Adapted code from perl FAQ in shell functions for single string and for a file (does not need rev):
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate commify a numeric string, perl in shell # functions.
# See: perldoc -q commas

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
cs() { echo "$1" | perl -wp -e '1 while s/^([-+]?\d+)(\d{3})/$1,$2/; ' ; }
cf() { perl -wp -e '1 while s/^([-+]?\d+)(\d{3})/$1,$2/; ' "$1" ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=${1-data1}

pl " With single string:"
for number in 1 12 123 1234 12345 123456 -1 -12 -123 -1234 +1 +12 +123 +1234
do
  pe " for $number, result is $( cs $number )"
done

pl " With contents of a file:"
head $FILE
pe
cf $FILE

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 With single string:
 for 1, result is 1
 for 12, result is 12
 for 123, result is 123
 for 1234, result is 1,234
 for 12345, result is 12,345
 for 123456, result is 123,456
 for -1, result is -1
 for -12, result is -12
 for -123, result is -123
 for -1234, result is -1,234
 for +1, result is +1
 for +12, result is +12
 for +123, result is +123
 for +1234, result is +1,234

-----
 With contents of a file:
999
1000
-1000
+1000
12345
1234567.1234
-1234567.1234
+1234567.1234
12345678911234567

999
1,000
-1,000
+1,000
12,345
1,234,567.1234
-1,234,567.1234
+1,234,567.1234
12,345,678,911,234,567

Best wishes ... cheers, drl
# 12  
Old 05-14-2012
using python 3.2:-

Code:
import re

x = '12345678911234567'
m = re.findall(r"(\d{2,3})",x[::-1])
y = ",".join(m)
result = y[::-1]
print(result)

Code:
output:

'12,345,678,911,234,567'

BR

SmilieSmilieSmilieSmilie
# 13  
Old 05-14-2012
Some awks (gawk, mawk, BSD awk) can do this:
Code:
echo 12345678911234567 | awk '{for(i=NF-3;i>=1;i-=3)$i=$i","}1' FS= OFS=

# 14  
Old 05-15-2012
actually if you want the number to be dynamic , my code will be better if you do below:-

Code:
import re

x = '12345678911234567'
m = re.findall(r"(\d{1,3})",x[::-1])
y = ",".join(m)
result = y[::-1]
print(result)

in this way , any number you give with even or odd count will give you a correct result.
BR
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract n-digits from string in perl

Hello, I have a log file with logs such as 01/05/2017 10:23:41 : file.log.38: database error, MODE=SINGLE, LEVEL=critical, STATE: 01170255 (mode main how can i use perl to extract the 8-digit number below from the string 01170255 Thanks (7 Replies)
Discussion started by: james2009
7 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. UNIX for Dummies Questions & Answers

How to search for a file having a particular character in a particular place in a directory.?

Hi Guys, I want to search for a specific file in a directory which have a "b" letter as the 3rd character in the name of the file. For Example : /abc/efg/ldbjfblkj.sh /abc/efg/erublkd.sh /abc/efg/eibueora.sh /abc/efg/kfvnmnb.sh Since we have 2 files with "b" as a 3rd character in... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

Terminate left side portion of a string

I have a awk file which consists of the follwoing code in file select.awk : /xxx/ { time = gensub(/xxx \*\*\*(.*)/, "\\1", "g") printf("%s\n",time) next } and an input file with the following file file.txt :- xxx ***Wed May 2 18:00:00 CDT 2012 AAA AAAA AAAA xxx... (4 Replies)
Discussion started by: shikshavarma
4 Replies

5. Shell Programming and Scripting

awk search between 2 digits a string

I would like to search between two a string. I thought this would be easy. The is always at the beginning of a line. The code: gawk '/^/{d=$1},/searchstring/,/^(d+1)/' or gawk '/^/,/searchstring/,/^/' did not return the desired result. inputfile.txt 999 some text searchstring some... (6 Replies)
Discussion started by: sdf
6 Replies

6. Shell Programming and Scripting

Insert a special character $ in place of extra spaces

Hi Experts, I have called some.txt with the following content. oracle HYRDSRVIHUB01 pts/0 TESTIHUB 07-JUN-10 CREATE TABLE TESTIHUB PHONE ... (12 Replies)
Discussion started by: naree
12 Replies

7. Shell Programming and Scripting

find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format. ua07app201 (20 Replies)
Discussion started by: honglus
20 Replies

8. Shell Programming and Scripting

Extract digits at end of string

I have a string like xxxxxx44. What's the best way to extract the digits (one or more) in a ksh script? Thanks (6 Replies)
Discussion started by: offirc
6 Replies

9. UNIX for Dummies Questions & Answers

to check if a string has only digits

Hi guys, I am not very experienced in writing ksh scripts and I am trying to write a piece of code that indicates if a given string contains only digits and no alphabet (upper or lower case). If i write it my way it would turn out to have a lot of comparisons.. :eek: Thanks a lot in... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

How to get the most left hand string ??

Hi, I remember once seeing a way to get the left most string in a word. Let's say: a="First.Second.Third" (separated by dot) echo ${a#*.} shows --> Second.Third echo ${a##*.} shows --> Third How do I get the the left most string "First" Or "First.Second" ??? Tried to replace #... (2 Replies)
Discussion started by: jfortes
2 Replies
Login or Register to Ask a Question