Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need scripting help inserting characters into string

Status
Not open for further replies.

deftones

Technical User
Jun 11, 2002
2
US
Hello all,

I cannot figure this out for the life of me. I have tried a couple of different methods to do this with awk's printf function but couldn't get it.

basically I have a 12 digit number that I want to place a decimal every 4 characters in the string.

i.e.
string1=abcd12345678
i would want it to be:
string1=abcd.1234.5678

also would like to know how to do the reverse.
thanks.
 
string2=`echo $string1 | cut -c1-4`.`echo $string1 | cut -c5-8`.`echo $string1 | cut -c 9-12`
 
ahh, cut command
thats what I was looking for, thanks.
 

echo "yourStringHere" | nawk -f dec.awk


#----------------------- dec.awk------------------

BEGIN {

}

{
len=length($0);
for (i=1; i <= len; i=i+4){
sstr=substr($0, i, 4);
str= (i==1) ? sstr : str &quot;.&quot; sstr
}

printf(&quot;str->[%s] dec->[%s]\n&quot;, $0, str);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top