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

How to create a file outputting fields in a fixed position 2

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi All,

Does anyone know the best way to do the following if its at all possible using awk or sed.

I have a file that contains 3 fields per transaction

e.g.
Field1,Field2,Field3

I want to recreate the file so that Field1 starts at potion 1 on the line, field2 starts at character position 42 and field 3 at character position 49 etc..

So each line on the file will look something like this: -

Field1 Field2 Field3
Field1 etc...

I will be very grateful of any help please.
Many thanks

 
look at printf command (formatted output)

man printf

HTH,

p5wizard
 
Hi,

how about something like this:

--------------------------------------------
#!/usr/bin/ksh

typeset -L16 No1
typeset -L16 No2
typeset -L16 No3

No1=$(cat 2 | awk '{print $1}' FS=,)
No2=$(cat 2 | awk '{print $1}' FS=,)
No3=$(cat 2 | awk '{print $1}' FS=,)

echo "$No1 $No2 $No3" >> newfile
--------------------------------------------

Remark:

"cat 2" means the infofile that I created. You'll have to change it according to what the file your fields are stored in is called.

typeset defines the length for each field.

Regards
Thomas
 
oops
there's a mistake ...

You'll have to change the script like this:

No1=$(cat 2 | awk '{print $1}' FS=,)
No2=$(cat 2 | awk '{print $2}' FS=,)
No3=$(cat 2 | awk '{print $3}' FS=,)


(It is of course not always $1)

Sorry.

Regards
Thomas
 
Thanks Thomas I will give this a go.

Cheers
 
Hiya,

I have tried this but is only good if there is only one record within your input file (Cat 2). I have a file that will contain, several records.

e.g

$1 $2 $3 $4
123452,1256235635662,124562463,121313131
134486,6883864133767,454646454,787994947

also the -L removes leading blanks and I need to keep these in.

Any other ideas may be good, if not thanks anyway.
 
In the awk man page have a look at the printf function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top