Reputation: 2677
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?
Upvotes: 1
Views: 61
Reputation: 10039
Depend on the recurrent strucutre (keyword). A sed solution
sed 's/\(:[0-9]\{2\}\).*\(- API\)/\1 \2/' YourFile
Upvotes: 1
Reputation: 12438
You can also use perl
or sed
to manipulate your file:
perl -pe 's/(?<=.{16}).*(?=-)//g' file
INPUT:
$ cat file
May 28 04:28:15 apps1 docker/[a:vjwdrze2e3qxrf5eck4s3yv:a][u:jack:u][5425]: 05:30:15.693 INFO [mockAppThread:21] app.js - TEST MESSAGE 3
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.1.0.0 with port 7001.
May 30 11:30:15 apps1 docker/[a:vjwdrze2e3qxrf5eck4s3yv:a][u:jack:u][5425]: 05:30:15.693 INFO [mockAppThread:21] app.js - TEST MESSAGE 2
OUPUT:
$ perl -pe 's/(?<=.{16}).*(?=-)//g' file
May 28 04:28:15 - TEST MESSAGE 3
May 29 01:30:15 - API Mock App Server started successfully on 0.1.0.0 with port 7001.
May 30 11:30:15 - TEST MESSAGE 2
Explanations:
This perl
command will delete the string starting at the 17th position until the reaching the -
.
Upvotes: 2
Reputation: 133458
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
Upvotes: 2