Find 1st occurance of string pattern and extract a substring from it
up vote
-1
down vote
favorite
I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.
Ex file:
This is a test
test EPMAT-20 ......
....
EPMAT.33 test
end of test.
sed
add a comment |
up vote
-1
down vote
favorite
I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.
Ex file:
This is a test
test EPMAT-20 ......
....
EPMAT.33 test
end of test.
sed
Please decide whether you wantEPMAT
searched orEMPAT
...
– user218374
Mar 26 '17 at 17:31
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.
Ex file:
This is a test
test EPMAT-20 ......
....
EPMAT.33 test
end of test.
sed
I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.
Ex file:
This is a test
test EPMAT-20 ......
....
EPMAT.33 test
end of test.
sed
sed
edited Nov 20 at 22:48
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Mar 26 '17 at 16:58
naren b
11
11
Please decide whether you wantEPMAT
searched orEMPAT
...
– user218374
Mar 26 '17 at 17:31
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17
add a comment |
Please decide whether you wantEPMAT
searched orEMPAT
...
– user218374
Mar 26 '17 at 17:31
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17
Please decide whether you want
EPMAT
searched or EMPAT
...– user218374
Mar 26 '17 at 17:31
Please decide whether you want
EPMAT
searched or EMPAT
...– user218374
Mar 26 '17 at 17:31
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
With sed
:
sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file
add a comment |
up vote
1
down vote
grep -m1 -oP 'bEPMAT-Kd+' yourfile
-m1
will just look at the first match in the whole of file
-P
will enable the Perl regex engine.
-o
will show only the matching portion
perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile
sed -ne '
/<EPMAT-[0-9][0-9]*/{
s//&
/
s/.*-([0-9]*n)/1/
P;q
}
' yourfile
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
With sed
:
sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file
add a comment |
up vote
1
down vote
With sed
:
sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file
add a comment |
up vote
1
down vote
up vote
1
down vote
With sed
:
sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file
With sed
:
sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file
answered Mar 26 '17 at 17:22
Satō Katsura
10.9k11534
10.9k11534
add a comment |
add a comment |
up vote
1
down vote
grep -m1 -oP 'bEPMAT-Kd+' yourfile
-m1
will just look at the first match in the whole of file
-P
will enable the Perl regex engine.
-o
will show only the matching portion
perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile
sed -ne '
/<EPMAT-[0-9][0-9]*/{
s//&
/
s/.*-([0-9]*n)/1/
P;q
}
' yourfile
add a comment |
up vote
1
down vote
grep -m1 -oP 'bEPMAT-Kd+' yourfile
-m1
will just look at the first match in the whole of file
-P
will enable the Perl regex engine.
-o
will show only the matching portion
perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile
sed -ne '
/<EPMAT-[0-9][0-9]*/{
s//&
/
s/.*-([0-9]*n)/1/
P;q
}
' yourfile
add a comment |
up vote
1
down vote
up vote
1
down vote
grep -m1 -oP 'bEPMAT-Kd+' yourfile
-m1
will just look at the first match in the whole of file
-P
will enable the Perl regex engine.
-o
will show only the matching portion
perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile
sed -ne '
/<EPMAT-[0-9][0-9]*/{
s//&
/
s/.*-([0-9]*n)/1/
P;q
}
' yourfile
grep -m1 -oP 'bEPMAT-Kd+' yourfile
-m1
will just look at the first match in the whole of file
-P
will enable the Perl regex engine.
-o
will show only the matching portion
perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile
sed -ne '
/<EPMAT-[0-9][0-9]*/{
s//&
/
s/.*-([0-9]*n)/1/
P;q
}
' yourfile
edited Mar 26 '17 at 17:30
answered Mar 26 '17 at 17:14
user218374
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f353978%2ffind-1st-occurance-of-string-pattern-and-extract-a-substring-from-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please decide whether you want
EPMAT
searched orEMPAT
...– user218374
Mar 26 '17 at 17:31
sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17