bash, awk, sed 또는 perl을 사용하여 선형 명령의 다음 줄에서 sdX만 캡처하는 방법은 무엇입니까?
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","
예상 출력
sdb
sdc
sdd
sde
sdf
답변1
당신은 그것을 사용할 수 있습니다그렙논쟁:
-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.
따라서 귀하의 명령은 다음과 같습니다
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*sd\w*"
sdb
sdc
sdd
sde
sdf
답변2
사용
echo ... | grep -Eo "sd[a-z]"
where는 -E
패턴을 (확장된) 정규식으로 해석하고 -o
각 줄에서 일치하는 부분만 인쇄합니다.
답변3
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 'sd\w'
sdb
sdc
sdd
sde
sdf
답변4
GNU sed:
$ s='"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 $s| sed -E ':b;s~[^,:]+.{,3}/rid/(.+)~\1~;Te;h;s~(\w+)/.*~\1~p;g;tb;:e d'