I want to substitute the p with "@" from 2nd occurrence to 4th occurrence.
sed 's/p/@/2g' file.txt
This command substitutes from the 2nd occurrence up to the last occurrence of "p". But 1 want to substitute from 2nd to 4th. So how to do it ?
Assuming this is bash or zsh, you can make use of brace expansion.
sed -e's/p/@/2'{,,} file
{,,} will repeat -e's/p/@/2' thrice, so it'll replace 2nd, 3rd and 4th ps.
{,,}
-e's/p/@/2'
p
Thanks this one works, is there any other way using sed command.
Perhaps
sed -e's/p/@/'{4..2} file
?