Computing.Net > Forums > Unix > Substring in Korn Shell Script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Substring in Korn Shell Script

Reply to Message Icon

Name: Sam
Date: September 10, 2003 at 18:13:21 Pacific
OS: Solaris
CPU/Ram: F50 / 10 G
Comment:

I have data in current format

20030910091500000000
20030910114500000000
20030910020000000000

I need to convert this to

2003-09-10-09.15.00.000000
2003-09-10-11.45.00.000000
2003-09-10-02.00.00.000000

Any suggestions - how I can do this??

Appreciate ur help..



Sponsored Link
Ads by Google

Response Number 1
Name: Nails
Date: September 10, 2003 at 20:39:01 Pacific
Reply:

Hi:

Since you're data is fixed length, you can do something like this:

while read x
do
f1=$(echo $x|cut -c1-4)
f2=$(echo $x|cut -c5-6)
f3=$(echo $x|cut -c7-8)
f4=$(echo $x|cut -c9-10)
f5=$(echo $x|cut -c11-12)
f6=$(echo $x|cut -c13-14)
f7=$(echo $x|cut -c15-20)
# printf all on one line
printf "%s-%s-%s-%s.%s.%s.%s\n" $f1 $f2 $f3 $f4 $f5 $f6 $f7
done data.file
# you need a 'less than sign' between done and the data file

Somebody can probably come up with a cool korn shell pattern to do this.

Regards,

Nails


0

Response Number 2
Name: WilliamRobertson
Date: September 11, 2003 at 06:23:42 Pacific
Reply:

Or you could do it with sed:

sed 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)\(......\)/\1-\2-\3-\4.\5.\6.\7/' data.file


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Substring in Korn Shell Script

korn shell script problem (using awk ? ) www.computing.net/answers/unix/korn-shell-script-problem-using-awk-/3547.html

using ftp in Korn Shell www.computing.net/answers/unix/using-ftp-in-korn-shell/4148.html

mkdir in korn shell cgi script www.computing.net/answers/unix/mkdir-in-korn-shell-cgi-script/5347.html