具有正则表达式和替换的Sed命令

时间:2017-12-28 12:04:53

标签: regex shell unix sed

我是sed命令和正则表达式的新手。

任何人都可以帮我解释下面的命令

 echo "FIXTEST_XYZ             23040/tcp       # 127.0.0.1:23040" | sed -n 's/^FIXTEST_\([A-Za-z0-9]\+\)\s\+\([0-9]\+\)\/tcp\s\+#\s*\([A-Za-z0-9.]\+\):\([0-9]\+\)\s*/\1 \2 \3 \4/p'

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

检查explainshell是否有命令解释。

检查regex101是否有正则表达式解释:

^FIXTEST_([A-Za-z0-9]+)\s+([0-9]+)\/tcp\s+#\s*([A-Za-z0-9.]+):([0-9]+)\s*

FIXTEST_ matches the characters FIXTEST_ literally (case sensitive)
1st Capturing Group ([A-Za-z0-9]+)
    Match a single character present in the list below [A-Za-z0-9]+
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
    a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
    0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([0-9]+)
    Match a single character present in the list below [0-9]+
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\/ matches the character / literally (case sensitive)
tcp matches the characters tcp literally (case sensitive)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
# matches the character # literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
3rd Capturing Group ([A-Za-z0-9.]+)
: matches the character : literally (case sensitive)
4th Capturing Group ([0-9]+)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
Global pattern flags
g modifier: global. All matches (don't return after first match)

第一捕获组匹配:XYZ

第二次捕获组匹配:23040

第三次捕获组匹配:127.0.0.1

第四次捕获组匹配:23040

Bash标准输出:XYZ 23040 127.0.0.1 23040