Text string modification

My shell needs to take a 10 digit number and output it in the forum nnn-nnn-nnnn I considered using the fold command but that won't help too much.

The following works in bash, at least:

a=${number:0:3}
b=${number:3:3}
c=${number:6:4}
echo "$a-$b-$c"

Any posix shell:

m=${nr#????};
echo ${nr%??????}-${m%???}-${nr#???????}

sed can do the job:

echo 1234567890 | sed 's/\(.\{3\}\)/&-/1;s/\(.\{7\}\)/&-/'
echo $nr|sed -r 's/(....)(...)(...)/\1-\2-\3/'
echo $nr|sed 's/\(....\)\(...\)\(...\)/\1-\2-\3/'
$ echo 1234567890 |awk '{print substr($0,1,3),substr($0,4,3),substr($0,7,4)}' OFS="-"
123-456-7890

Shorter :wink:

echo 1234567890 | awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:$i)}' FS=

Shorter :wink:

echo 1234567890 | awk '{print $1$2$3$4"-"$5$6$7"-"$8$9$10}' FS=

Although you write with shorter code, but the code is not more useful, if the input file has more than 1 line. :slight_smile:

$ cat urfile 
1234567890
0987654321

$ awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:$i)}' FS= urfile
123-456-7890098-765-4321

And I don't understand why not use "for(i=1;i<=NF;i++)". Just for shorter code?

All other commands, which are run by awk, sed and echo, are still correct.

$ awk '{print substr($0,1,3),substr($0,4,3),substr($0,7,4)}' OFS="-" urfile
123-456-7890
098-765-4321

$ sed 's/\(.\{3\}\)/&-/1;s/\(.\{7\}\)/&-/' urfile
123-456-7890
098-765-4321

Ops, right :wink:

awk '{for(i=0;++i<=NF;)printf ((i~/4|7/)?"-"$i:((i==NF)?$i RS:$i))}' FS= file
# echo 1234567890| gawk -vFS= '{$3=$3"-";$6=$6"-"}1' OFS=
123-456-7890

thank you
is there a setting I can change so that I can get notified via e-mail after every new post and not just the first reply?