awk 在兩個特定字串之間讀取並丟棄其餘部分

awk 在兩個特定字串之間讀取並丟棄其餘部分

我過去awk只讀取兩個字串“Check”和“Result”之間的文字。我已經使用了在互聯網上找到的許多變體,但仍然無法獲得理想的結果。我試過了:

awk "/Check:/,/Result:/ {print}"  BMSCA209-040-transfer-report.18-Jun-2014.11:18.csv.tmp | more

我也嘗試過:

sed -n "/Check:/,/Result:/p" BMSCA209-040-transfer-report.18-Jun-2014.11:18.csv.tmp | more

但仍然沒有得到我想要的。每次我得到它,這就是我得到的:

ata>    <data fieldName="Timepoint ID" value="B01 SCREENING"/>  <data fieldName="SQCSummary" value=" Nothing Submission Quality and Compliance Report - 201
4-06-03T14:30:00.547-07:00Check: Ensure slice thickness is between 2mm and 5mmResult: FailReason: Image(s) found with slice thickness out of range.   Instanc
e 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.369 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.139541
7628.479.368 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.367 found with slice thickness out
of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.366 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.
55.3.4094358250.93.1395417628.479.365 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.364 found
with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.363 found with slice thickness out of range : 1.25   I
nstance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.362 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.
1395417628.479.361 found with slice thickness out of range : 1.25   Instance 1.2.840.113619.2.55.3.4094358250.93.1395417628.479.360 found with slice thicknes

有人還有其他建議嗎?

答案1

perl

perl -l -0777 -ne 'print for /Check: (.*?)Result:/gs' < file

對於 GNU grep,(幾乎)等價的是:

grep -zPo '(?s)Check: \K.*?(?=Result:)' < file

或與pcregrep

pcregrep -Mo1 '(?s)Check: (.*?)Result:' < file

輸出:

Ensure Modality is the same for all images in a DICOM series.
Ensure SeriesDate is in the proper DICOM format (YYYYMMDD) for all images.
[...]

答案2

我對你的問題的解決方案:

使用grepbash 字串操作如下:

RES="$(cat BMSCA209-040-transfer-report.18-Jun-2014.11:18.csv.tmp |  egrep -o 'Check.*Result')"
RES=${RES%Result}
RES=${RES#Check: }
echo $RES

就是這樣 :)

結果是:

Ensure slice thickness is between 2mm and 5mm

相關內容