Linux 如果文件中有匹配的字符串,则获取部分行的文本
Linux get part of line's text if matched string in files
在 Linux 中,我有一个文件 test.log 与此类似:
2021/11/18 17:19:18,034 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-702","correlationID":"b67601e81bfd","requestData":"TYPE_1, REQUEST 1"}
2021/11/18 17:19:18,036 INFO {"queueName":"queue/ResponseQueue","correlationID":"TMYHxIyeYo","responseData":"TYPE_1, RESPONSE 1"}
2021/11/18 17:19:18,038 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-885","correlationID":"j9BNzbbv3E","requestData":"TYPE_2, REQUEST 2"}
2021/11/18 17:19:19,172 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-183","correlationID":"d29d2d7cf5a4","requestData":"TYPE_1, REQUEST 3"}
2021/11/18 17:19:20,784 INFO {"queueName":"queue/esbRequestQueue",{"threadName":"WorkManager(2)-762","correlationID":"4235eba2765a","requestData":"TYPE_1, REQUEST 4"}
我想搜索此文件的所有行并获取包含字符串 ["requestData":"TYPE_1] 的行,并将 requestData 的所有内容写入另一个名为 [=20= 的文件]。test.log 文件中的每个匹配行将写入 type_1.txt 文件中的一行。下面是我在 type_1.txt 文件中的预期结果:
TYPE_1, REQUEST 1
TYPE_1, REQUEST 3
TYPE_1, REQUEST 4
我的问题是有没有可以做到这一点的命令?我是 Linux 的新手,所以请帮助我。
试试这个命令:
grep -Po '(?<="requestData":")TYPE_1[^"]*' test.log > type_1.txt
在 Linux 中,我有一个文件 test.log 与此类似:
2021/11/18 17:19:18,034 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-702","correlationID":"b67601e81bfd","requestData":"TYPE_1, REQUEST 1"}
2021/11/18 17:19:18,036 INFO {"queueName":"queue/ResponseQueue","correlationID":"TMYHxIyeYo","responseData":"TYPE_1, RESPONSE 1"}
2021/11/18 17:19:18,038 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-885","correlationID":"j9BNzbbv3E","requestData":"TYPE_2, REQUEST 2"}
2021/11/18 17:19:19,172 INFO {"queueName":"queue/RequestQueue",{"threadName":"WorkManager(2)-183","correlationID":"d29d2d7cf5a4","requestData":"TYPE_1, REQUEST 3"}
2021/11/18 17:19:20,784 INFO {"queueName":"queue/esbRequestQueue",{"threadName":"WorkManager(2)-762","correlationID":"4235eba2765a","requestData":"TYPE_1, REQUEST 4"}
我想搜索此文件的所有行并获取包含字符串 ["requestData":"TYPE_1] 的行,并将 requestData 的所有内容写入另一个名为 [=20= 的文件]。test.log 文件中的每个匹配行将写入 type_1.txt 文件中的一行。下面是我在 type_1.txt 文件中的预期结果:
TYPE_1, REQUEST 1
TYPE_1, REQUEST 3
TYPE_1, REQUEST 4
我的问题是有没有可以做到这一点的命令?我是 Linux 的新手,所以请帮助我。
试试这个命令:
grep -Po '(?<="requestData":")TYPE_1[^"]*' test.log > type_1.txt