简体   繁体   中英

how to combine multiple awk commands using different options

I need some help in joining the below 3 awk commands using different options.

  • awk -F/ 'BEGIN {print "SPK REPO"} {print $7," ",$9}' - To extract 6th and 8th field
  • awk -v RS=' ' 'BEGIN {print "log4j-version"} '/^1/''} - To extract the version in 13th field.
  • awk -F'[][]' 'BEGIN {print "Timestamp"} {print $2}' - To extract datetime stamp from 13th field.

Input Text file format : /hosting/cbj/shared/master/jobs/TAIS/jobs/tais_aem_build/branches/feature-Dev/builds/214/log:[2022-06-23T07:23:56.117Z] ch.qos.logback.classic.log4j 0 1.2.3 com.baml.tais.tais_aem_build:jar:6.4.0

I need to extract the 6th, 8th, 13th (date-timestamp), 13th (1.2.3 version) and print them into a csv file with headers. Sample Output :

SPK REPO log4j-version Timestamp
TAIS tais_aem_build 1.2.3 2022-06-23T07:23:56.117Z

Using multiple delimiters

awk -F'/|\\[|] ?| ' '
   BEGIN{
      OFS=";"
      print "SPK","REPO","log4j-version","Timestamp"
   }
   {
      print $7,$9,$18,$15
   }
 ' file

SPK;REPO;log4j-version;Timestamp
TAIS;tais_aem_build;1.2.3;2022-06-23T07:23:56.117Z

Or changing all delimiters to /

awk -F/ '
   BEGIN{
      OFS=";"
      print "SPK","REPO","log4j-version","Timestamp"
  }
  {
     gsub(/[][ ]/, "/"); 
     gsub("//", "/"); 
     print $7,$9,$18,$15
  }
' file

SPK;REPO;log4j-version;Timestamp
TAIS;tais_aem_build;1.2.3;2022-06-23T07:23:56.117Z

Or using split

awk '
   BEGIN{
      OFS=";"
      print "SPK","REPO","log4j-version","Timestamp"
   }
   {
      n=split($1,a,"/")
      print a[7],a[9], $4, gensub(/.*\[(.*)\].*/,"\\1",1,a[n])
   }
 ' file

SPK;REPO;log4j-version;Timestamp
TAIS;tais_aem_build;1.2.3;2022-06-23T07:23:56.117Z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM