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!

Changing FS mid program 1

Status
Not open for further replies.

richardii

Programmer
Jan 8, 2001
104
GB
I'm using the program below to format a file into a CSV. For most of the data | or ^ is the FS, but for the comments fields the string .br would be the FS. I can't seem to put the reassignment of the FS in the right place to make it work! U thought if I reassign just after if($4=="REP") and then put FS back to it's original value it'd be okay, but no. The program is cut down, and may not work as it stands.
Thanks.

BEGIN {

FILENAME="IVMS.AWK" ## Set filename
FS = "[|^]" ## Set initial field separator
OFS="," ## Output field separator
DFS="/" ## Date field separator
COMMASUB="~" ## Comma substitution value
DEBUG=1 ## Debug switch - 1 to enable, 0 to disable
LAST_CELL=62 ## Must equal the number of cells listed above
FIRST_RECORD=1 ## First record flag
RS=chr(13)

if (DEBUG) {
print "Awk script : " FILENAME
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print "Debug : Enabled"
print "Output field separator : " OFS
print "Input field separator : " FS
print "Date field separator : " DFS
print "Commas in text replaced by : " COMMASUB
print "Record Seperator : " RS
print "\n=================================\n\n"
}
COUNT=0
}

#remove any qualifiers present
{gsub(&quot;<&quot;,&quot;&quot;)}
{gsub(&quot;>&quot;,&quot;&quot;)}
{gsub(/\+/,&quot;&quot;)}
{gsub(&quot;-&quot;,&quot;&quot;)}



#remove any commas
{gsub(&quot;,&quot;,&quot;&quot;)}

function chr(c)
{
# force c to be numeric by adding 0
return sprintf(&quot;%c&quot;, c + 0)
}

## Process line starting with 'FTS'
/FTS/ {

##Print values of last record as L is the last line marker
if(COUNT!=0) {
for(CELL=1;CELL<LAST_CELL-1;CELL++) {
##This is the comma seperated output
printf(&quot;%s%s&quot;,POSITION[CELL],OFS)
}
printf(&quot;%s\n&quot;,POSITION[LAST_CELL])
delete POSITION[LAST_CELL]
}
}

## Process line starting with 'PID'
/PID/ {

##Print previous record's values as P is the beginning of next record marker
if(COUNT!=0) {
for(CELL=1;CELL<LAST_CELL-1;CELL++) {
printf(&quot;%s%s&quot;,POSITION[CELL],OFS)
}
printf(&quot;%s\n&quot;,POSITION[LAST_CELL])
delete POSITION[LAST_CELL]

}

##Clear previous record's values so they aren't copied

for(CELL=1;CELL<LAST_CELL-1;CELL++) {
delete POSITION[CELL]
}



## Increment counter
COUNT=COUNT+1

## Save row details for this line
## if the middle name is there this will be true




POSITION[1]=$28
POSITION[2]=$6
POSITION[3]=$7
POSITION[4]=substr($12,7,2) DFS substr ($12,5,2) DFS substr($12,1,4)
POSITION[33]=$13
POSITION[34]=$16
POSITION[35]=$17
POSITION[36]=$18
POSITION[37]=$19&quot; &quot;$20&quot; &quot;$21
}




## Process line starting with 'OBR'
/OBR/ {

DOS=substr($9,7,2) DFS substr ($9,5,2) DFS substr($9,1,4)
if (DEBUG) {
print &quot;Date of Sample : &quot; DOS

}
## Save row details for this line
POSITION[5]=DOS

}



## Process specific lines

##HAEMOGLOBIN

{if($4==&quot;7187&quot;){

if (DEBUG) {
print &quot;Haemoglobin : &quot; $8
}

POSITION[6]=$8
}}



##COMMENTS

{if($4==&quot;REP&quot;){

if (DEBUG) {
print &quot;Comment1 : &quot; $8
}
## Save row details for this line
POSITION[26]=$8

}}

}}
END



 
Can't you replace .br with another char which is not used in the source files.
It will not be a significant slow-down.

Regards Gregor.
Gregor.Weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top