如何从文本文件中挑选选定的文本,并通过排序将其写入另一个文本文件.(使用脚本)



我有一个文件,它看起来像这样(myfile.txt(

{"total":25,"p":1,"ps":100,"paging":{"pageIndex":1,"pageSize":100,"total":25},
{"key":"AYKBKVWSknsgv2StRWde","rule":"squid:UselessImportCheck","severity":"MINOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/service/impl/ExportIncidentDataServiceImpl.java","project":"ADK-FileService","line":24,"hash":"e989c030a8526c7abdb57e7ffdb44d19","textRange":{"startLine":24,"endLine":24,"startOffset":0,"endOffset":62},"flows":[],"status":"OPEN","message":"Remove this unused import 'com.qt.emp.dashboard.adkfileservice.dao.FileServiceDao'.","effort":"2min","deqt":"2min","author":"jason.roy@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization
{"key":"AYKBKVqQknsgv2StRWdf","rule":"squid:S1068","severity":"MAJOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/util/Utility.java","project":"ADK-FileService","line":48,"hash":"c85bd39c3602eb4d7a9db9537c74467c","textRange":{"startLine":48,"endLine":48,"startOffset":29,"endOffset":47},"flows":[],"status":"OPEN","message":"Remove this unused "INCIDENT_REFERENCE" private field.","effort":"5min","deqt":"5min","author":"james.david@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization
{"key":"AYKBKVqQknsgv2StRWdg","rule":"squid:S1068","severity":"MAJOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/util/Utility.java","project":"ADK-FileService","line":49,"hash":"27fa1f5b351545c577aeedab469716a0","textRange":{"startLine":49,"endLine":49,"startOffset":29,"endOffset":41},"flows":[],"status":"OPEN","message":"Remove this unused "qt_REFERENCE" private field.","effort":"5min","deqt":"5min","author":"james.david@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization
{"key":"AYKBKVqQknsgv2StRWdh","rule":"squid:S1068","severity":"MAJOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/util/Utility.java","project":"ADK-FileService","line":50,"hash":"a94988f90fa13232a41a41bef72d4d89","textRange":{"startLine":50,"endLine":50,"startOffset":29,"endOffset":47},"flows":[],"status":"OPEN","message":"Remove this unused "CUSTOMER_REFERENCE" private field.","effort":"5min","deqt":"5min","author":"james.david@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization
{"key":"AYKBKVqQknsgv2StRWdi","rule":"squid:S1068","severity":"MAJOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/util/Utility.java","project":"ADK-FileService","line":51,"hash":"c7ed73b2755815ed022f238c9ea46c9f","textRange":{"startLine":51,"endLine":51,"startOffset":29,"endOffset":47},"flows":[],"status":"OPEN","message":"Remove this unused "CUSTOMER_REF_ALIAS" private field.","effort":"5min","deqt":"5min","author":"james.david@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization
{"key":"AYKBKVqQknsgv2StRWdj","rule":"squid:S1068","severity":"MAJOR","component":"ADK-FileService:src/main/java/com/qt/emp/dashboard/adkfileservice/util/Utility.java","project":"ADK-FileService","line":52,"hash":"0e11af0c921aaa4535d971662ebbaa92","textRange":{"startLine":52,"endLine":52,"startOffset":29,"endOffset":40},"flows":[],"status":"OPEN","message":"Remove this unused "ISSUEDETAIL" private field.","effort":"5min","deqt":"5min","author":"james.david@qt.com","tags":["unused"],"creationDate":"2022-08-09T06:52:13+0100","updateDate":"2022-08-16T13:37:28+0100","type":"CODE_SMELL","organization":"default-organization

我必须写";键"项目"作者"类型";值转换为引用myfile.txt 的新文件(mynewfile.txt(

则输出将是(所需输出(

AYKBKVWSknsgv2StRWde    ADK-FileService     james.david@qt.com      CODE_SMELL
AYKBKVqQknsgv2StRWdf    ADK-FileService     james.david@qt.com      CODE_SMELL
AYKBKVqQknsgv2StRWdg    ADK-FileService     james.david@qt.com      CODE_SMELL
AYKBKVqQknsgv2StRWdh    ADK-FileService     james.david@qt.com      CODE_SMELL
AYKBKVqQknsgv2StRWdi    ADK-FileService     james.david@qt.com      CODE_SMELL
AYKBKVqQknsgv2StRWdj    ADK-FileService     james.david@qt.com      CODE_SMELL

我试过这个

while read p; do
sed -nE 's/^(.*"key":")([^"]*)(","rule".*)$/2/p' > mynewfile.txt
done < myfile.txt

则输出为

AYKBKVWSknsgv2StRWde
AYKBKVqQknsgv2StRWdf
AYKBKVqQknsgv2StRWdg
AYKBKVqQknsgv2StRWdh
AYKBKVqQknsgv2StRWdi
AYKBKVqQknsgv2StRWdj

它拥有";键";仅限值。我必须写";项目"作者"类型";值。

有人能帮我获得所需的输出吗。提前谢谢。

第一个解决方案: 使用GNUawk和您显示的示例和尝试,请尝试以下代码。

awk '
BEGIN{ OFS="t" }
match($0,/"key":"([^"]*)".*component":"([^:]*):.*"author":"([^"]*)".*,"type":"([^"]*)"/,arr){
print arr[1],arr[2],arr[3],arr[4]
}
' Input_file | column -t -s $'t'


第二个解决方案:使用sed,您可以尝试以下代码。使用-E选项在此处启用ERE。用GNUsed编写和测试。

sed -E 's/.*{"key":"([^"]*)".*component":"([^:]*):.*"author":"([^"]*)".*,"type":"([^"]*)".*$/1t2t3t4/' Input_file

最新更新