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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Commas to a field 1

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
GB
In the text file below I would like commas inserted after the 1st , 3rd, 5th, 6th characters;

from this;
20101A60
20102A53
20103A46
20104A39
20105A32
20106A25
20107A18
20108A11
to something like this;
2,01,01,A,60
2,01,02,A,53
2,01,03,A,46
2,01,04,A,39
2,01,05,A,32
2,01,06,A,25
2,01,07,A,18
Thanks for your help in advance
 
use

awk '{ print substr($0,1,1)&quot;,&quot;substr($0,2,2)&quot;,&quot;substr($0,4,2)&quot;,&quot;substr($0,6,1)&quot;,&quot;substr($0,7)}' < input file name > output file name

That should do the trick SOL
Yeah people they won't understand,
Girlfriends they don't understand,
In spaceships they don't even understand,
and me I aint ever gonna understand
 
Thanks SOL that solved it
NOGS
 
SOL
How exactly does substr work, I may need it for further work and cant find any details in man pages

NOGS
 
Here's the excerpt from my man pages, it basically allows you to slice up strings.

substr(s, m[, n])
Return the at most n-character substring of s
that begins at position m, numbering from 1. If
n is omitted, the substring is limited by the
length of strings. SOL
Yeah people they won't understand,
Girlfriends they don't understand,
In spaceships they don't even understand,
and me I aint ever gonna understand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top