Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl exclude words with pattern

Tags:

bash

sed

awk

perl

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

like image 989
macder Avatar asked Sep 11 '26 01:09

macder


1 Answers

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
like image 61
RavinderSingh13 Avatar answered Sep 13 '26 16:09

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!