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!

shell script

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am writing shell script and i need some help from you guys to figure out to add ";" in a string. Here is the example

if i give a string (123456) then the out put should be
(12;34;56)

How can i do that. Pls guide me

Thanks in advance
 
Hi,

assuming you deal with six digit strings you can do:

(a) Korn shell script:

#! /bin/ksh

print -n "enter your string: "

read STRING

STRING1=${STRING%%[0-9][0-9][0-9][0-9]}
STRING2temp=${STRING##[0-9][0-9]}
STRING2=${STRING2temp%%[0-9][0-9]}
STRING3=${STRING##[0-9][0-9][0-9][0-9]}

NEWSTRING="$STRING1;$STRING2;$STRING3"

print $NEWSTRING

exit 0

(b) Perl script:

#! /usr/bin/perl

print "enter your string: ";

chomp ($string = <STDIN>);

$string =~ /((\d{2})(\d{2})(\d{2}))/;
@parts = ($2, $3, $4);
$string_with_semicolon= join(';', @parts);

print &quot;$string_with_semicolon\n&quot;;

Maybe somebody else has some more sophisticated solutions?

mrjazz [pc2]
 
Dear mrjazz

Thanks for your script.

I figured out another way:

echo &quot;1234567890&quot;|sed 's/\(..\)/&:/g'

Thanks for your help.

reg

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top