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("<",""}
{gsub(">",""}
{gsub(/\+/,""}
{gsub("-",""}
#remove any commas
{gsub(",",""}
function chr(c)
{
# force c to be numeric by adding 0
return sprintf("%c", 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("%s%s",POSITION[CELL],OFS)
}
printf("%s\n",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("%s%s",POSITION[CELL],OFS)
}
printf("%s\n",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" "$20" "$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 "Date of Sample : " DOS
}
## Save row details for this line
POSITION[5]=DOS
}
## Process specific lines
##HAEMOGLOBIN
{if($4=="7187"{
if (DEBUG) {
print "Haemoglobin : " $8
}
POSITION[6]=$8
}}
##COMMENTS
{if($4=="REP"{
if (DEBUG) {
print "Comment1 : " $8
}
## Save row details for this line
POSITION[26]=$8
}}
}}
END
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("<",""}
{gsub(">",""}
{gsub(/\+/,""}
{gsub("-",""}
#remove any commas
{gsub(",",""}
function chr(c)
{
# force c to be numeric by adding 0
return sprintf("%c", 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("%s%s",POSITION[CELL],OFS)
}
printf("%s\n",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("%s%s",POSITION[CELL],OFS)
}
printf("%s\n",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" "$20" "$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 "Date of Sample : " DOS
}
## Save row details for this line
POSITION[5]=DOS
}
## Process specific lines
##HAEMOGLOBIN
{if($4=="7187"{
if (DEBUG) {
print "Haemoglobin : " $8
}
POSITION[6]=$8
}}
##COMMENTS
{if($4=="REP"{
if (DEBUG) {
print "Comment1 : " $8
}
## Save row details for this line
POSITION[26]=$8
}}
}}
END