如何从行中捕获特定字符串

如何从行中捕获特定字符串

如何在一个线性命令中使用 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

您可以使用GREP论点:

   -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'

相关内容