-3

I am new to sed command and regular expressions.

Could anyone help me to explain the below command

 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'

Thank you for your help !!

Phani
  • 3
  • 3

1 Answers1

0

Check explainshell for command explaination.

Check regex101 for regex explanation:

^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)

1st Capturing Group matches: XYZ

2nd Capturing Group matches: 23040

3rd Capturing Group matches: 127.0.0.1

4th Capturing Group matches: 23040

Bash standard output: XYZ 23040 127.0.0.1 23040

Ben
  • 4,631
  • 3
  • 16
  • 26