複数のファイルから正確な文字列を同時に把握する方法

複数のファイルから正確な文字列を同時に把握する方法

以下のように、フォルダに複数のファイル(テキストファイル)があります。ここで、最初のファイルには一部のパスが文字列として含まれ、他のファイルにはパス+ファイル名が含まれています。

ファイル1:

[root@centos7 test]# pwd
/root/test
[root@centos7 test]# cat file1.txt
/abc/bce/12345/input/part3
/abc/bce/12345/input/part3/err
/abc/bce/34563/input
/abc/bce/34563/input/part1
/abc/bce/34563/input/part3/wrk
/abc/bce/11198/input/VII
/abc/bce/11198/input/VII/err
/abc/bce/11198/input/VII/part3
/abc/bce/11198/input/VII/part3/err
[root@centos7 test]#

ファイル2:

[root@centos7 test]# pwd
/root/test/test
[root@centos7 test]# cat file2.txt
/abc/bce/12345/input/part3/AIR9905.txt--20210421--
/abc/bce/12345/input/part3/AIR9923.txt--20210315--
/abc/bce/12345/input/part3/err/AIR9950.txt--20200512--
[root@centos7 test]#

ファイル3:

[root@centos7 test]# pwd
/root/test/test
[root@centos7 test]# cat file3.txt
/abc/bce/12345/input/part3/err/AIR1034.txt--20210110--
/abc/bce/34563/input/part1/AIR3426.txt--20200420--
/abc/bce/11198/input/VII/part3/err/V.AIR7650.txt--20170625--
[root@centos7 test]#

現在の出力:

test/file2.txt:/abc/bce/12345/input/part3/AIR9905.txt--20210421--
test/file2.txt:/abc/bce/12345/input/part3/AIR9923.txt--20210315--
test/file2.txt:/abc/bce/12345/input/part3/err/AIR9950.txt--20200512--
test/file3.txt:/abc/bce/12345/input/part3/err/AIR1034.txt--20210110--

予想出力:

test/file2.txt:/abc/bce/12345/input/part3/AIR9905.txt--20210421--
test/file2.txt:/abc/bce/12345/input/part3/AIR9923.txt--20210315--

私はgrep -rHw "/abc/bce/12345/input/part3" test/file1の行を一致させ、file2、file3などからその情報を抽出するために使用します。しかし、問題は、行を検索しようとすると、file2、file3などから同様の行をすべて取得することです。 file1を複数のファイルと連続して比較するときにこれを行う方法がわかりません。

ベストアンサー1

file2.txtあなたの質問を正しく解釈したら、あなたの例で行を探したいと思いますfile3.txtいいえその他 "/abc/bce/12345/input/part3/ を含むよく文字列。私が知る限り、あなたが望むものは少なくともgrep単一のコマンドでは不可能です。

その理由は、同時にするgrepクエリに基づいて長い文字列を探している場合(例: "/abc/bce/12345/input/part3"は "test/file2.txt:/abc/bce/12345/input/part3/AIR9905.txt-- 「と一致する)20210421--」)といいえgrepクエリに基づいて長い文字列を探している場合(例: "/abc/bce/12345/input/part3"は "test/file2.txt:/abc/bce/12345/input/part3/"と一致します)よく/AIR9950.txt--20200512---")。

あなたが提示した特定の例ではこれを行うことができますが、明らかにすべてのケースgrep -rHw "/abc/bce/12345/input/part3" test/ | grep -v "/err"で機能する普遍的な解決策ではありません。

おすすめ記事