mercredi 1 avril 2015

In AWK, how to insert the value of variable as string at the beginning of the line

Problem solved: it is due to the hidden "ctrl-Ms". I used the following awk code to remove them:



awk '{ gsub("\r", ""); print $0;}'


I try to insert a variable as a string at the beginning lines with a match.


The updated example, between &&&&&& lines, which is a bit long but needed for explaining my codes. I try to extract some information from each case (with unique case # as SP 15 xxxx). I want to insert case numbers at the front of each lines of diagnosis (using the variable of "old_case")


&&&&&&



MEDICAL RECORD | SURGICAL PATHOLOGY Pg 1 of 2

PATHOLOGY REPORT Accession No. SP 15 1111

Submitted by: Date obtained:

Specimen (Received ):
RIGHT, RIGHT RIGHT RIGHT

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

PATHOLOGY REPORT Accession No. SP 15 1111

Gross description:
The specimen is received in one formalin filled container, labeled

Microscopic Diagnosis:
Lung, right right right right, left left
DX: CHANGES CHANGES CHANGES
NO EVIDENCE OF MALIGNANCY
(PLEASE SEE DESCRIPTION)

/es/ signature, M.D.


&&&&&&


My whole script is as follows:



#! /bin/awk -f

BEGIN { old_case = "0000"; }

/Accession No./ {if (old_case != $7) {old_case = $7; print "Acc#",$5,$6,$7 > "output"} else {}};
/Microscopic Diagnosis:/ {flag=1;next} /\/es\// || /SUPPLEMENTARY/ {flag=0} flag {print old_case,$0 > "output"};

END { }


The result I got is:



Acc# SP 15 1111
1111
Lung, right right right right, left left
1111
DX: CHANGES CHANGES CHANGES
1111
NO EVIDENCE OF MALIGNANCY
1111
(PLEASE SEE DESCRIPTION)
1111
1111


It appears that a new line breaker was added each time the variable "old_case" printed. On the other hand, if I use just a string "test" instead of variables (with the following code)



/Microscopic Diagnosis:/ {flag=1;next} /\/es\// {flag=0} flag {print "test",$0 > "output"};


the result is as follows, which is what I want to get:



Acc# SP 15 1111
test Lung, right right right right, left left
test DX: CHANGES CHANGES CHANGES
test NO EVIDENCE OF MALIGNANCY
test (PLEASE SEE DESCRIPTION)
test
test


I am using Mac OSX 10.9.5


Aucun commentaire:

Enregistrer un commentaire