Unix Shell - How to: Insert character into fixed size "number"?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > How to: Insert character into fixed size "number"?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to: Insert character into fixed size "number"?
di98mase

2006-01-13, 10:40 pm

Hi,

I have a script where I use a variable that is a number. The number is
always 10 digits. How do I insert a '-' sign between the 7 and 8 digit?

For example I want:

"1234567890" to be "1234567-890"...

Regards

Di

Thobias Vakayil

2006-01-13, 10:40 pm

di98mase wrote:

>Hi,
>
>I have a script where I use a variable that is a number. The number is
>always 10 digits. How do I insert a '-' sign between the 7 and 8 digit?
>
>For example I want:
>
>"1234567890" to be "1234567-890"...
>
>Regards
>
>Di
>
>
>


x="1234567890"
echo $x | awk '{printf("%s-%s",substr($1,1,7),substr($1,8,3))}'

Regards,

Thobias Vakayil

Stephane Chazelas

2006-01-13, 10:40 pm

On 12 Jan 2006 02:26:32 -0800, di98mase wrote:
[...]
> I have a script where I use a variable that is a number. The number is
> always 10 digits. How do I insert a '-' sign between the 7 and 8 digit?
>
> For example I want:
>
> "1234567890" to be "1234567-890"...

[...]

insert_dash() {
eval "
$1=\${$1%???}-\${$1#???????}
"
}

var=1234567890
insert_dash var
printf '%s\n' "$var"

--
Stephane
Andrew McDermott

2006-01-13, 10:40 pm

"di98mase" <di98mase@hotmail.com> wrote in
news:1137061592.490614.171130@g44g2000cwa.googlegroups.com:

> Hi,
>
> I have a script where I use a variable that is a number. The
> number is always 10 digits. How do I insert a '-' sign between the
> 7 and 8 digit?
>
> For example I want:
>
> "1234567890" to be "1234567-890"...
>
> Regards
>
> Di
>


newvar=`echo $oldvar | sed 's/...$/-&/'`

Andrew
Ed Morton

2006-01-13, 10:40 pm

di98mase wrote:

> Hi,
>
> I have a script where I use a variable that is a number. The number is
> always 10 digits. How do I insert a '-' sign between the 7 and 8 digit?
>
> For example I want:
>
> "1234567890" to be "1234567-890"...
>
> Regards
>
> Di
>


Parameter substitution:

$ x="1234567890"
$ echo "${x%???}-${x#???????}"
1234567-890

or, if you prefer:

$ x="1234567890"
$ y="${x%???}"
$ echo "${y}-${x#$y}"
1234567-890

Regards,

Ed.
grulos

2006-01-13, 10:40 pm

di98mase wrote:
> "1234567890" to be "1234567-890"...


using bash
x="1234567890"
$ echo "${x:0:7}-${x:7:3}"
1234567-890

Stephane Chazelas

2006-01-13, 10:40 pm

On 12 Jan 2006 06:59:22 -0800, grulos wrote:
> di98mase wrote:
>
> using bash


Note that this is a ksh93 feature copied by bash.

> x="1234567890"
> $ echo "${x:0:7}-${x:7:3}"
> 1234567-890


The equivalent in zsh would be:

x=1234567890
print $x[1,7]-$x[8,10]

zsh didn't copy the ${var:n:n} feature as it conflicts with the
${var:-n} feature inherited from the Bourne shell and with the
${var:modifiers} inherited from csh and tcsh. zsh is able to
subscript a string variable as it's got a consistent typing
where scalars and arrays are distinguishible (in ksh/bash,
scalars are arrays with only one indice 0, though it's not
totally clear in bash prior to version 3, so that ${string[0]}
would return the content of $string and any other indice will
return nothing).

--
Stephane
romy

2006-01-20, 8:13 am

if your "1234567890" is at start of line than use :

sed -e 's/^[[:digit:]]\{7\}/&-/g' filename


or if you eant all 10 digit number to be converted to above mentioned
for mat then use :

sed -e 's/[[:digit:]]\{7\}/&-/g' filename

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com