Home > other >  How to pick selected text from a text file and write it into another text file with sorting. (using
How to pick selected text from a text file and write it into another text file with sorting. (using

Time:08-24

I have a file which looks like this (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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","tags":["unused"],"creationDate":"2022-08-09T06:52:13 0100","updateDate":"2022-08-16T13:37:28 0100","type":"CODE_SMELL","organization":"default-organization

I have to write "key", "project", "author", "type" values to a newfile(mynewfile.txt) referring myfile.txt

then output would be (required output)

AYKBKVWSknsgv2StRWde    ADK-FileService     [email protected]      CODE_SMELL
AYKBKVqQknsgv2StRWdf    ADK-FileService     [email protected]      CODE_SMELL
AYKBKVqQknsgv2StRWdg    ADK-FileService     [email protected]      CODE_SMELL
AYKBKVqQknsgv2StRWdh    ADK-FileService     [email protected]      CODE_SMELL
AYKBKVqQknsgv2StRWdi    ADK-FileService     [email protected]      CODE_SMELL
AYKBKVqQknsgv2StRWdj    ADK-FileService     [email protected]      CODE_SMELL

I tried this

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

then output is

AYKBKVWSknsgv2StRWde
AYKBKVqQknsgv2StRWdf
AYKBKVqQknsgv2StRWdg
AYKBKVqQknsgv2StRWdh
AYKBKVqQknsgv2StRWdi
AYKBKVqQknsgv2StRWdj

It is having "key" values only. I have to write "project", "author", "type" values either.

can someone help me to get required output. Thanks in advance.

CodePudding user response:

1st solution: With GNU awk and your shown samples and attempts please try following code.

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'


2nd solution: With sed you could try following code. Using -E option to enable ERE here. Written and tested in GNU sed.

sed -E 's/.*\{"key":"([^"]*)".*component":"([^:]*):.*"author":"([^"]*)".*,"type":"([^"]*)".*$/\1\t\2\t\3\t\4/' Input_file
  • Related