how to capture specific strings from line
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
how to capture only the sdX from the following line ( withe bash/awk/sed/perl one liner )
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","
expected output
sdb
sdc
sdd
sde
sdf
awk sed perl
add a comment |Â
up vote
1
down vote
favorite
how to capture only the sdX from the following line ( withe bash/awk/sed/perl one liner )
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","
expected output
sdb
sdc
sdd
sde
sdf
awk sed perl
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
how to capture only the sdX from the following line ( withe bash/awk/sed/perl one liner )
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","
expected output
sdb
sdc
sdd
sde
sdf
awk sed perl
how to capture only the sdX from the following line ( withe bash/awk/sed/perl one liner )
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","
expected output
sdb
sdc
sdd
sde
sdf
awk sed perl
edited Dec 22 '17 at 12:02
FaxMax
423219
423219
asked Dec 22 '17 at 9:48
yael
2,0091145
2,0091145
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
2
down vote
accepted
You can use GREP arguments:
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression
(PCRE). This is experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
So your command would be:
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | grep -oP "w*sdw*"
sdb
sdc
sdd
sde
sdf
1
Sorry, you talk about the option-P
and the you use-h
to hide the filename?grep
is (in this case) reading fromstdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
add a comment |Â
up vote
1
down vote
Use
echo ... | grep -Eo "sd[a-z]"
where -E
interprets the pattern as an (extended) regular expression and -o
prints only the matching parts in each line.
add a comment |Â
up vote
0
down vote
You did not specify what x
should be in sdX
so one could as well assume that it might be any character:
$ echo echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs /data"," | grep -o 'sd.'
sdb
sdc
sdd
sde
sdf
A literal .
means any character in regular expressions.
And in perl
because you wanted that explicitly:
$ echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | perl -nE 'say $& while /sd./g'
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
0
down vote
Above result is achieved by awk one liner
echo " ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","" | sed "s/,/n/g" | awk -F "/" 'print $3' | sed '/^$/d'
output
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
-1
down vote
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",'
"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",' | grep -Po 'sdw'
sdb
sdc
sdd
sde
sdf
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use GREP arguments:
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression
(PCRE). This is experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
So your command would be:
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | grep -oP "w*sdw*"
sdb
sdc
sdd
sde
sdf
1
Sorry, you talk about the option-P
and the you use-h
to hide the filename?grep
is (in this case) reading fromstdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
add a comment |Â
up vote
2
down vote
accepted
You can use GREP arguments:
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression
(PCRE). This is experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
So your command would be:
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | grep -oP "w*sdw*"
sdb
sdc
sdd
sde
sdf
1
Sorry, you talk about the option-P
and the you use-h
to hide the filename?grep
is (in this case) reading fromstdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use GREP arguments:
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression
(PCRE). This is experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
So your command would be:
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | grep -oP "w*sdw*"
sdb
sdc
sdd
sde
sdf
You can use GREP arguments:
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression
(PCRE). This is experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
So your command would be:
echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | grep -oP "w*sdw*"
sdb
sdc
sdd
sde
sdf
edited Dec 22 '17 at 11:04
answered Dec 22 '17 at 10:41
Kevin Lemaire
1,037421
1,037421
1
Sorry, you talk about the option-P
and the you use-h
to hide the filename?grep
is (in this case) reading fromstdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
add a comment |Â
1
Sorry, you talk about the option-P
and the you use-h
to hide the filename?grep
is (in this case) reading fromstdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
1
1
Sorry, you talk about the option
-P
and the you use -h
to hide the filename? grep
is (in this case) reading from stdin
â FaxMax
Dec 22 '17 at 10:48
Sorry, you talk about the option
-P
and the you use -h
to hide the filename? grep
is (in this case) reading from stdin
â FaxMax
Dec 22 '17 at 10:48
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
Corrected, wrong copy/paste. Thanks.
â Kevin Lemaire
Dec 22 '17 at 10:52
add a comment |Â
up vote
1
down vote
Use
echo ... | grep -Eo "sd[a-z]"
where -E
interprets the pattern as an (extended) regular expression and -o
prints only the matching parts in each line.
add a comment |Â
up vote
1
down vote
Use
echo ... | grep -Eo "sd[a-z]"
where -E
interprets the pattern as an (extended) regular expression and -o
prints only the matching parts in each line.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Use
echo ... | grep -Eo "sd[a-z]"
where -E
interprets the pattern as an (extended) regular expression and -o
prints only the matching parts in each line.
Use
echo ... | grep -Eo "sd[a-z]"
where -E
interprets the pattern as an (extended) regular expression and -o
prints only the matching parts in each line.
answered Dec 22 '17 at 10:48
elm
1134
1134
add a comment |Â
add a comment |Â
up vote
0
down vote
You did not specify what x
should be in sdX
so one could as well assume that it might be any character:
$ echo echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs /data"," | grep -o 'sd.'
sdb
sdc
sdd
sde
sdf
A literal .
means any character in regular expressions.
And in perl
because you wanted that explicitly:
$ echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | perl -nE 'say $& while /sd./g'
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
0
down vote
You did not specify what x
should be in sdX
so one could as well assume that it might be any character:
$ echo echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs /data"," | grep -o 'sd.'
sdb
sdc
sdd
sde
sdf
A literal .
means any character in regular expressions.
And in perl
because you wanted that explicitly:
$ echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | perl -nE 'say $& while /sd./g'
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You did not specify what x
should be in sdX
so one could as well assume that it might be any character:
$ echo echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs /data"," | grep -o 'sd.'
sdb
sdc
sdd
sde
sdf
A literal .
means any character in regular expressions.
And in perl
because you wanted that explicitly:
$ echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | perl -nE 'say $& while /sd./g'
sdb
sdc
sdd
sde
sdf
You did not specify what x
should be in sdX
so one could as well assume that it might be any character:
$ echo echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs /data"," | grep -o 'sd.'
sdb
sdc
sdd
sde
sdf
A literal .
means any character in regular expressions.
And in perl
because you wanted that explicitly:
$ echo ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data"," | perl -nE 'say $& while /sd./g'
sdb
sdc
sdd
sde
sdf
edited Dec 22 '17 at 12:15
answered Dec 22 '17 at 11:03
Arkadiusz Drabczyk
7,21521532
7,21521532
add a comment |Â
add a comment |Â
up vote
0
down vote
Above result is achieved by awk one liner
echo " ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","" | sed "s/,/n/g" | awk -F "/" 'print $3' | sed '/^$/d'
output
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
0
down vote
Above result is achieved by awk one liner
echo " ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","" | sed "s/,/n/g" | awk -F "/" 'print $3' | sed '/^$/d'
output
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Above result is achieved by awk one liner
echo " ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","" | sed "s/,/n/g" | awk -F "/" 'print $3' | sed '/^$/d'
output
sdb
sdc
sdd
sde
sdf
Above result is achieved by awk one liner
echo " ""dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data","" | sed "s/,/n/g" | awk -F "/" 'print $3' | sed '/^$/d'
output
sdb
sdc
sdd
sde
sdf
answered Dec 23 '17 at 9:54
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
up vote
-1
down vote
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",'
"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",' | grep -Po 'sdw'
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
-1
down vote
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",'
"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",' | grep -Po 'sdw'
sdb
sdc
sdd
sde
sdf
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",'
"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",' | grep -Po 'sdw'
sdb
sdc
sdd
sde
sdf
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",'
"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",
echo '"dfs.datanode.data.dir" : "/rid/sdb/oo/hdfs/data,/rid/sdc/oo/hdfs/data,/rid/sdd/oo/hdfs/data,/rid/sde/oo/hdfs/data,/rid/sdf/oo/hdfs/data",' | grep -Po 'sdw'
sdb
sdc
sdd
sde
sdf
answered Dec 22 '17 at 10:37
FaxMax
423219
423219
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412456%2fhow-to-capture-specific-strings-from-line%23new-answer', 'question_page');
);
Post as a guest
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
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
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