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

2022-10-13 18:49:15标签linuxshellsedgrepcut
提问

我有一个像这样的文件(myfile。txt) 我必须将“key”“key”“项目”“作者”的“类型”值写入一个新文件(mynewfile。txt)引用myfile。txt 然后输出(所需输出) 我试过这个 然后是输出 它只有“关键”值。我必须写“项目”的作者“类型”的价值观。 有人能帮我获得所需的输出吗?提前谢谢。 第1个解决方案:用GNU awk和您的显示的示例和尝试,请尝试以下代码。 第二种解决方案:在sed中,您可以尝试以下代码。使用-E选项在这里启用。在GNU sed中编写和测试。

{"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

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

回答

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 's/.*\{"key":"([^"]*)".*component":"([^:]*):.*"author":"([^"]*)".*,"type":"([^"]*)".*$/\1\t\2\t\3\t\4/' Input_file

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5