I have Filename which contains a lot of strings, but I need to cut only specific names and exclude other garbage from the string
Example of File:
FAILED, see /release/jenkins/workspace/Build/RELEASE/logs/component.jdfmfh_value_javac10+.log
FAILED, see /release/jenkins/workspace/Build/RELEASE/logs/component.jadxfh_value_javac10+.log
FAILED, see /release/jenkins/workspace/Build/RELEASE/logs/component_value_javac10+.log
FAILED, see /release/jenkins/workspace/Build/RELEASE/logs/component_value_javac10+.log
FAILED, see /release/jenkins/workspace/Build/RELEASE/logs/component.jdfmfh_value_javac10+.log
So I need to get result like:
component.jdfmfh
component.jadxfh
component
component
component.jdfmfh
I wrote small perl expression and got close result, but I don't know how to exclude all lines _value_javac10+.log from there.
perl -pe 's/^.*\/logs\///;' Filename
P.S. If there's a way to do it through the sed, that works for me as well
With your shown samples please try following solutions.
1st solution: Using GNU awk and its match function which creates a capturing group into array arr and printing its first item as per requirement.
awk 'match($0,/.*\/([^_]*)_/,arr){print arr[1]}' Input_file
2nd solution: Using GNU grep here with regex to obtain the required output.
grep -oP '^.*\/\K[^_]*' Input_file
3rd solution: Using field separator as / and splitting last field with _ and printing very first element of array as per needed output.
awk -F'/' 'split($NF,arr,"_"){print arr[1]}' Input_file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With