0

I am trying to understand one of the bash scrip. There I landed at below sed command, as far as I know, sed replaces first set with 2nd set, but I am confusing at [ ]*, could you please help me what it does.

complete command.

sed 's/[ ]*|||[ ] */|||/g'

I have executed command by taking a sample file , wherever it see 3 pipes , it is replacing with again ||| pipes.

If that so, I am not able to understand the command, why do we replace same characters

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Sriharsha
  • 5
  • 2
  • Looks like it removes space ( trailing and leading before and after of ||| – Sriharsha Dec 28 '22 at 03:48
  • Yes, it is. But it only replaces single space. If any file have `|||` with more than one space at beginning and end then the above command will only remove single space. – Gaurav Pathak Dec 28 '22 at 07:54

1 Answers1

1

This part [ ]* is a character class (which you can also write as * without the square brackets) and matches optional spaces, in this case to the left and the right of the 3 pipes.

Then replaces it with 3 pipes only without the surrounding spaces.

echo "test ||| and ||||| and   | || ||| " | sed 's/[ ]*|||[ ]*/|||/g'

Output (tested on macOs 13.1 and Ubuntu GNU sed 4.8)

test|||and||||| and   | |||||
The fourth bird
  • 154,723
  • 16
  • 55
  • 70