Search code examples
shellawksedsubstr

Command to get different strings from a line in shell


I have a text file. Some of the sample lines in the file are as below

MESS01: Java flow 'com.java.abc.SupportToolsOutput' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.ErrorNotify' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.Output' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.LogRequest' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.Router' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.ProcessMessageNextGen' on jvm group 'JVM123' is running.
MESS01: Java flow 'com.java.abc.RouteMessage' on jvm group 'JVM123' is stopped.`

I am tryng to get the output using a single shell command as

com.java.abc.SupportToolsOutput,running
com.java.abc.ErrorNotify,running
com.java.abc.Output,running 
com.java.abc.LogRequest,running
com.java.abc.Router,running
com.java.abc.ProcessMessageNextGen,running
com.java.abc.RouteMessage,stopped`

I tried using substr and awk.

I tried cat textfile.txtt|awk '{print substr($4,2,length($4)-1)}'|sed "s/'/ /g" and cat textfile.txt|awk '{ print $4,$10 }'|sed "s/'/ /g" but can't get the desired result.

Please help.

Update: If my text file is something like this

MESS01: Java flow 'com.java.abc.SupportToolsOutput' on jvm group 'JVM123' is running.

Additional thread instances: '0'
Deployed: '1/11/12 2:44 AM' in Bar file '/www/deploy/JVM123/SupportToolsOutputDEV_2012-01-11_02-44-27.bar'
Last edited: '1/10/12 5:02 PM'
UUID: 'f9a9f0cb-3401-0000-0080-b85eb6410185'
Start mode: 'Maintained'
Long description: ''
User-defined property names:
  'BackOutThreshold' = '1'
  'LogLevel' = 'ERROR'
  'MaxPerInterval' = '5'
  'NotificationInterval' = '300'
Keywords:

--------
MESS01: Java flow 'com.java.abc.ErrorNotify' on jvm group 'JVM123' is running.

Additional thread instances: '0'
Deployed: '1/11/12 2:45 AM' in Bar file '/www/deploy/JVM123/ErrorNotifyDEV_2012-01-11_02-45-45.bar'
Last edited: '1/10/12 5:04 PM'
UUID: 'efcff1cb-3401-0000-0080-b85eb6410185'
Start mode: 'Maintained'
Long description: ''
User-defined property names:
  'LogLevel' = 'ERROR'
  'MaxPerInterval' = '5'

Keywords:

--------
MESS01: Java flow 'com.java.abc.Output' on jvm group 'JVM123' is running.

Additional thread instances: '0'
Deployed: '1/11/12 2:46 AM' in Bar file '/www/deploy/JVM123/OutputDEV_2012-01-11_02-46-44.bar'
Last edited: '1/10/12 3:30 PM'
UUID: '1fbbf2cb-3401-0000-0080-b85eb6410185'
Start mode: 'Maintained'
Long description: ''
User-defined property names:
  'BackOutThreshold' = '1'
  'BasicAuthorization' = 'YWRtaW46cGFzc3dvcmQ='
  'LogLevel' = 'ERROR'
  'MaxPerInterval' = '5'
  'NotificationInterval' = '300'
  'ProxyAuthorization' = 'QTkwNzk2MzpnNzVuajZqcQ=='
  'isSslSecured' = 'FALSE'
Keywords:

--------
MESS01: Java flow 'com.java.abc.LogRequest' on jvm group 'JVM123' is running.

Additional thread instances: '4'
Deployed: '1/11/12 2:48 AM' in Bar file '/www/deploy/JVM123/LogRequestDEV_2012-01-11_02-48-54.bar'
Last edited: '1/10/12 4:00 PM'
UUID: '60b4f4cb-3401-0000-0080-b85eb6410185'
Start mode: 'Maintained'
Long description: ''
User-defined property names:
  'EVENTTYPE' = 'Integration_RequestSent'
  'LogLevel' = 'ERROR'
  'MaxPerInterval' = '5'
  'NotificationInterval' = '300'
  'SOARTMCompliant' = 'FALSE'
Keywords:

--------
MESS01: Java flow 'com.java.abc.Router' on jvm group 'JVM123' is stopped.

Additional thread instances: '4'
Deployed: '1/11/12 2:49 AM' in Bar file '/www/deploy/JVM123/RouterDEV_2012-01-11_02-49-32.bar'
Last edited: '1/10/12 4:10 PM'
UUID: '8d46f5cb-3401-0000-0080-b85eb6410185'
Start mode: 'Maintained'
Long description: ''
User-defined property names:
  'BackOutThreshold' = '1'
  'LogLevel' = 'ERROR'
  'MaxPerInterval' = '5'
  'NotificationInterval' = '300'
Keywords:
--------
MESS02 : Java file 'Integration.jar' on on jvm group 'JVM123'. 
Deployed: '1/11/12 2:46 AM' in Bar file '/www/deploy/JVM123/OutputDEV_2012-01-11_02-46-44.bar'
Last edited: '1/10/12 4:10 PM'
Keywords:

--------
MESS02 : Java file 'SAPAdapter.adapter' on on jvm group 'JVM123'. 
Deployed: '1/11/12 2:46 AM' in Bar file '/www/deploy/JVM123/OutputDEV_2011-11-10_22-55-55.bar'
Last edited: '1/10/14 14:55 PM'
Keywords:

And i want my output as

JVM123,/www/deploy/JVM123/OutputDEV_2012-01-11_02-46-44.bar,Integration.jar
JVM123,/www/deploy/JVM123/OutputDEV_2011-11-10_22-55-55.bar,SAPAdapter.adapter
JVM123,/www/deploy/JVM123/SupportToolsOutputDEV_2012-01-11_02-44-27.bar,com.java.abc.SupportToolsOutput,running
 JVM123,/www/deploy/JVM123/ErrorNotifyDEV_2012-01-11_02-45-45.bar,com.java.abc.ErrorNotify,running
 JVM123,/www/deploy/JVM123/OutputDEV_2012-01-11_02-46-44.bar,com.java.abc.Output,running 
JVM123,/www/deploy/JVM123/LogRequestDEV_2012-01-11_02-48-54.bar,com.java.abc.LogRequest,running
 JVM123,/www/deploy/JVM123/RouterDEV_2012-01-11_02-49-32.bar,com.java.abc.Router,stopped

Solution

  • This one grabs state on seeing MESS01 and prints on Deployed. It uses a simple and direct regex for field delimiter:

    LC_ALL=C awk -F "[ ']" -v OFS=, '/^MESS01:/ { sub(/\.$/,""); o=$5; j=$11; s=$NF; } /^Deployed:/ { print j, $(NF-1), o, s }' textfile.txt
    

    Tested it... and it produces what you want. Notes:

    • No cat was harmed in the mangling of this script. ;)
    • Your log looks to be all ASCII and using LC_ALL=C will likely make the script dramatically faster.
    • Using NF helps to avoid any issues with the date and is intuitive (in the answer, I am telling the reader that my intent in the code is to look at the last field in one case and next to last field in the other).
    • The reason for the sub() in MESS01 is that adding a "." in the delimiter regex would kill your object.
    • In the future, you may find it better to just use space for the delimiter and filter out ^'|'$ in jvm, object and path using gsub() -- in this case you would want to pass the filtering regex as a variable (due to quote confusion).

    Here is a version using the gsub() mentioned in the last point:

    LC_ALL=C awk -v OFS=, -v r="^'|'$" '/^MESS01:/ { o=$4; j=$8; s=$NF; gsub(r,"",o); gsub(r,"",j); sub(/\.$/,"",s) } /^Deployed:/ { p=$NF; gsub(r,"",p); print j, p, o, s }' textfile.txt