【问题标题】:Calling external command in find and using pipe在查找和使用管道中调用外部命令
【发布时间】:2022-01-05 02:08:52
【问题描述】:

我想知道是否有一种方法可以在 AIX 6.x 上使用 find 命令搜索某个目录(包括子目录)中的所有文件,然后调用外部命令(例如 hlcat)将它们显示/转换为可读格式,然后可以通过 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.

简而言之,我正在查找命令中寻找一种简洁明了的单行方法,并使用圆括号名称查看和搜索这些 HL7 文件的内容。

非常感谢, 乔治

附: HL7 原始数据由一条连续的线组成,除非使用 hlcat 等工具将其转换为可行的读取格式,否则无法读取。 在

【问题讨论】:

  • 你试过了吗? find . -type f -name “*.hl7” -exec hlcat {} \; | grep -l “pattern”
  • find . -exec a-script-that-encapsulates-the-complex-command {} \;.
  • 我正在寻找其中包含该模式的文件。当前目录中有这些患者的文件。 bash-4.3$ 找到 . -type f -name "患者.*" -exec hlcat {} \; | egrep -l "Barry|Jolene" (标准输入) (标准输入) ……… bash-4.3$ cat search_patient.sh #!/usr/local/bin/bash hlcat $1 | awk -F"|" '{ if ($1 == "^PID" && ($5 ~ /Barry|Jolene/)) { print FILENAME }}' bash-4.3$ find . -type f -name "* Patient*.*" -exec ./search_patient.sh {} \; # 没有找到这个病人的文件。 bash-4.3$ 它没有找到包含患者的文件。
  • search_patient.sh 如何拾取 find 命令找到的每个文件? find 是否将每个文件作为参数(例如 $1)一一传递给 search_patient.sh?

标签: bash unix aix


【解决方案1】:

更新:简单的方法

find . -type f -name '*.hl7' -exec grep -iEl 'Barry|Jolene' {} +

注意:您可能会得到一些误报。有针对性的搜索见下文。


在一堆 HL7v2 文件中搜索名字

1。查看 HL7v2 文件格式

HL7 示例v2 PID 段:

PID|||56782445^^^UAReg^PI||KLEINSAMPLE^BARRY^Q^JR||19620910|M|||

PID段分解:

Seq NAME HHIC USE LEN
0 PID keyword Segment Type 3
3 Patient ID Medical Record Num 250
5 Patient Name Last^First^Middle 250
7 Date/Time Of Birth YYYYMMDD 26
8 Sex F, M, or U 1
2。编写有针对性的搜索

使用grep (AIX):

find . -type f -name '*.hl7' -exec grep -iEl '^PID\|([^|]*\|){4}[^^|]*\^(Barry|Jolene)\^' {} +

awk:

find . -type f -name '*.hl7' -exec awk -v firstname='^(Barry|Jolene)$' '
    BEGIN { FS="|" }
    FNR == 1 { if( found ) print filename; found = 0; filename = FILENAME }
    $1 == "PID" { split($6, name, "^"); if (toupper(name[2]) ~ toupper(firstname)) { found = 1 } }
    END { if ( found ) print filename }
' {} +

备注:这个awk 解决方案的好处在于您将名字正则表达式作为参数传递。此解决方案易于扩展,例如用于搜索姓氏

【讨论】:

  • bash-4.3$ 查找 . -type f -name "*患者." -exec sh -c 'for file;做 hlcat "$file" | egrep -l "巴里|乔琳"; done' {} + (标准输入) (标准输入) ……….. bash-4.3$ find . -type f -name "患者.*" -exec sh -c 'for file;做 hlcat "$file" | egrep -q -l "Barry|Jolene" && printf "%s\n" "$file";完成' {} + file1.hl7 file2.hl7 …………。 bash-4.3$ 找到 . -type f -name "患者.*" -exec hlcat {} + | egrep -q -l "Barry|Jolene" bash-4.3$
  • -q 保留(标准输入)但对搜索没有任何作用。 -print0 确实存在于 AIX 中,并且不会为列出的每个文件打印换行符。我可以使用 -print(带换行符)。
  • 到目前为止,上述建议均未奏效。感谢您的努力。
  • @GeorgeJackson 在我的回答中添加了示例。还更正了 find 嵌入的 -exec sh -c 脚本的错误,该脚本在 {} + 之前需要一个虚拟参数
  • 以下建议对我有用: find 。 -type f -name '*.hl7' -exec sh -c ' for f;做猫“$f”| egrep -q "Barry|Jolene" && printf "%s\n" "$f"; done ' _ {} + 非常感谢您的努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2023-03-24
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
相关资源
最近更新 更多