2

I am reading a sed tutorial at https://riptutorial.com/sed/example/13753/lines-matching-regular-expression-pattern.

Looks like

$ sed -n '/[A-Z]/ s| |/|gp' ip.txt

is filtering 'Add Sub Mul Div' out of the file, and convert it to 'Add/Sub/Mul/Div'

I really don't understand the regex considering I just read https://www.tldp.org/LDP/abs/html/x23170.html.

It does not even match the print syntax which is:

[address-range]/p

and is the pipe sign '|' here alternation?

Could anyone explain:

'/[A-Z]/ s| |/|gp'

in English?

Edit

I also found that the extra empty space before 's' and after '/' is allowed and does not do anything. the correct syntax should be:

[address-range]/s/pattern1/pattern2/

the syntax check of sed pattern is not strict, and confusing

Hamada
  • 1,836
  • 3
  • 13
  • 27
user1559625
  • 2,583
  • 5
  • 37
  • 75

1 Answers1

5
  • -n option turns off automatic printing
  • sed allows to qualify commands with an address filtering, which could be regex or line addresses
    • for example, /foo/ d will delete lines containing foo
    • and /foo/ s/baz/123/ will change baz to 123 only if the line also contains foo
  • /[A-Z]/ match only lines containing at least one uppercase alphabet
Sundeep
  • 23,246
  • 2
  • 28
  • 103