我想知道在呼叫外部命令(例如 hlcat)以將它們顯示/轉換為可讀格式之前,是否有一種方法可以在 AIX 6.x 上使用 find 命令從某個目錄(包括子目錄)中搜索所有檔案然后可以通過 grep 命令來查找模式而不是在 shell 中使用回圈嗎?
e.g. find . -type f -name “*.hl7” -exec hlcat {} | grep -l “pattern” \;
上面的命令不起作用,我必須使用 while 回圈來顯示內容并搜索模式,如下所示:
find . -type f -name “*.hl7” -print | while read file; do
hlcat $file | grep -l “pattern”;
done
同時,這些 HL7 檔案已用圓括號重命名,以防止打開它們而不必在檔案名周圍包含雙引號。
e.g. hlcat (patient) filename.hl7 will fail to open.
hlcat “(patient) filename.hl7” will work.
簡而言之,我正在 find 命令中尋找一種干凈簡潔的單行方法,并使用圓括號名稱查看和搜索這些 HL7 檔案的內容。
非常感謝,喬治
PS HL7 原始資料由一條連續線組成,除非使用 hlcat 等工具將其轉換為可行的讀取格式,否則無法讀取。在
uj5u.com熱心網友回復:
查找包含pattern以下內容的 HL7 檔案:
- 使用 bash:
#!/bin/bash
find . -type f -name '*.hl7' -exec printf '%s\0' {} |
while IFS='' read -r -d '' filepath
do
hlcat "$filepath" | egrep -q 'pattern' && printf '%s\n' "$filepath"
done
注意: AIXfind沒有這個-print0選項
- 或者里面有一個腳本
find:
find . -type f -name '*.hl7' -exec sh -c '
for f; do
hlcat "$f" | egrep -q pattern && printf "%s\n" "$f";
done
' _ {}
注意: pattern必須正確插入腳本中(可能會導致可讀性降低)
更新:更換hlcat同cat向您展示它是如何作業
考慮以下內容簡單的檔案:
檔案名:
(Barry) fileX.hl7
內容:Barry檔案名:
(John) fileY.hl7
內容:John檔案名:
(Jolene) fileZ.hl7
內容:Jolene
命令:
find . -type f -name '*.hl7' -exec cat {} \; | egrep -l 'Barry|Jolene'
輸出:
(standard input)
這意味著egrep在其標準輸入中找到了模式...!這就是它所能做的一切,您的awk腳本將做一些等效的事情。
而命令:
find . -type f -name '*.hl7' -exec sh -c '
for f; do
cat "$f" | egrep -q "Barry|Jolene" && printf "%s\n" "$f";
done
' _ {}
和
#!/bin/bash
find . -type f -name '*.hl7' -exec printf '%s\0' {} |
while IFS='' read -r -d '' filepath
do
cat "$filepath" | egrep -q 'Barry|Jolene' && printf '%s\n' "$filepath"
done
兩個輸出:
./(Jolene) fileZ.hl7
./(Barry) fileX.hl7
哪些是包含模式的檔案。
Now if you're fine without a robust handling of filenames, then your example with the while loop could work, provided that you add double quotes when expanding the filename:
find . -type f -name '*.hl7' -print | while read file; do
cat "$file" | egrep -q 'Barry|Jolene' && printf '%s\n' "$file"
done
outputs:
./(Jolene) fileZ.hl7
./(Barry) fileX.hl7
But what's the point of not creating bullet-proof code when you can?
uj5u.com熱心網友回復:
在查看HL7 檔案格式后,我可以說您根本不需要hlcat在 HL7 v2檔案中查找患者名字:
- 使用 AIX 在所有檔案中搜索
grep:
find . -type f -name '*.hl7' -exec grep -iEl '^PID\|([^|]*\|){4}[^|]*\^(Barry|Jolene)\^' {}
- 在檔案中一一搜索,使用
awk:
find . -type f -name '*.hl7' -exec awk -v regexp='^(Barry|Jolene)$' '
BEGIN { FS="|" }
$1 == "PID" { split($6, name, "^"); if (toupper(name[2]) ~ toupper(regexp)) { found = 1 } }
END { exit !found }
' {} \; -print
- 在所有檔案中搜索,只需一次
awk呼叫:
find . -type f -name '*.hl7' -exec awk -v regexp='^(Barry|Jolene)$' '
BEGIN { FS="|" }
FNR == 1 { if( found ) print filename; found = 0; filename = FILENAME }
$1 == "PID" { split($6, name, "^"); if (toupper(name[2]) ~ toupper(regexp)) { found = 1 } }
END { if ( found ) print filename }
' {}
限制:如果您有 HL7 v3檔案,則上述命令都不起作用。
評論:
使用 bash,find當沒有大量檔案時,您可以擺脫:
#!/bin/bash
shopt -s globstar
grep -iEl '^PID\|([^|]*\|){4}[^|]*\^(Barry|Jolene)\^' ./**/*.hl7
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/369588.html
上一篇:如何在Jetpackcompose中檢測Horizo??ntalPager中的滑動?
下一篇:從字串C中洗掉字符
