3

I tried replacing 30 chars from the beginning of each line of my text, but could not figure out how to specify to vi sed to do it.

I tried this:

:%s/.{3}/xx/

But it could not find my pattern. I thought {3} would mean 3 times the previous, but unfortunately not. { is not interpreted as a regex command but as a search char.

could anyone help?

2 Answers 2

5

If you look at :h magic:

Examples:

after:    \v       \m       \M       \V         matches 
                'magic' 'nomagic'
          $        $        $        \$         matches end-of-line
          .        .        \.       \.         matches any character
          *        *        \*       \*         any number of the previous atom
          ~        ~        \~       \~         latest substitute string
          ()       \(\)     \(\)     \(\)       grouping into an atom
          |        \|       \|       \|         separating alternatives
          \a       \a       \a       \a         alphabetic character
          \\       \\       \\       \\         literal backslash
          \.       \.       .        .          literal dot
          \{       {        {        {          literal '{'
          a        a        a        a          literal 'a'

The default node in regular expressions is \m (just magic), so you need to use \{ for the brace to seen as an operator:

                                        /\{ E58 E60 E554 E870
\{n,m}  Matches n to m of the preceding atom, as many as possible
\{n}    Matches n of the preceding atom
\{n,}   Matches at least n of the preceding atom, as many as possible
\{,m}   Matches 0 to m of the preceding atom, as many as possible

Try:

%s/.\{30}/xx/
2
  • Thanks! This works for me under Linux , but not AIX. btw nothig with :h magic on my systems.
    – J. Chomel
    Apr 8, 2016 at 9:33
  • 1
    under AIX it says Invalid character in interval expression ; but if I escape the second one, it works.
    – J. Chomel
    Apr 8, 2016 at 9:36
3

In AIX vi, this will be:

:%s/.\{30\}/xx/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.