Search code examples
stringbashawksedcut

how to find the string from the line in bash?


I have string as below,

May 29 01:30:15 apps1 docker/[a:vjwdrze2e3qxrf5eck4s3yv:a][u:jack:u][5425]: 05:30:15.693 INFO  [mockAppThread:21] app.js - API Mock App Server started successfully on 0.0.0.0 with port 7001.

As an output, I want only timestamp and the error message.

May 29 01:30:15 API Mock App Server started successfully on 0.0.0.0 with port 7001.

I can get the timestamp by awk command as below.

awk '{print $1,$2,$3}'

I can get the message as below,

cut -d "-" -f2

How can I get both in a single command? Is there any simplified way to get it?


Solution

  • Following awk may help you here.

    awk '{sub(/ apps1.*app\.js/,"")} 1'  Input_file
    

    Solution 2nd:

    awk -F" app.js - " '{sub(/ apps1.*/,"",$1);print $1,$2}'   Input_file
    

    Solution 3rd: As per Subeh's suggestion in comments adding 1 more solution now too.

    awk -F'apps1|app.js -' '{ print $1 $3 }' Input_file