]*>/ /gp\" What is gp?It looks for non-greater-than characters preceding a greater-than symbol, and changes all of them to a single" />
开发者

what does this sed commands does? please explain its bits and pieces

开发者 https://www.devze.com 2023-02-01 03:31 出处:网络
Plea开发者_如何学JAVAse explain this sed command? sed -n \"s/[^>]*>/ /gp\" What is gp?It looks for non-greater-than characters preceding a greater-than symbol, and changes all of them to a single

Plea开发者_如何学JAVAse explain this sed command?

sed -n "s/[^>]*>/ /gp"

What is gp?


It looks for non-greater-than characters preceding a greater-than symbol, and changes all of them to a single space. Thus, it will convert this input (where I've used _ to indicate a space):

foo>_bar> b
x>>_a

to

___b
___a

As Mark notes, "g" means global, and "p" means "print the line".


  • g means global: i.e. replace all occurences, not just the first.
  • p means to print the modified line. Otherwise due to the -n switch it would not be printed.

The command finds all lines containing at least one > and prints some spaces followed by the text after the final >. The number of spaces printed is the number of > in the line.

For example if this line is in the input file:

123>456>789

Then this is printed:

  789


I was typing up a long explanation, but Brian beat me to it. To clarify a tiny bit, the "p" prints the modified / matching line. The "-n" in your command line tells sed to "not print the file". Combined with the "p", it works kinda like grep, but within the scope of the script (ie, anything it changes/matches).

0

精彩评论

暂无评论...
验证码 换一张
取 消