Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

With this entry from our crontab

1 3 1 * * find /var/log/tomcat/* -mtime +61 -type f -delete

I want to match the 5th space char so I can separate the command part from the time field part. Cron can have different formats, so I want to be able to match DOW as sun or 0.

The regular expression I am using in vim that matches the first space is and replaces with place holder "T".

:g/\*/s/\(\ \)\{1\}/T/

What am I missing to match the nth or 5th space. This does does nothing

:g/\*/s/\(\ \)\{5\}/T/

I want to generate a break down of when commands are run in cron to try and balance out the load on our servers.


I understand what I was doing wrong with the above search / substitutions.

(\ \)\{5\}

That gets expanded to five spaces after each other and not the 5th space as there are chars between the spaces.

This work up to the fifth space but the replacement "\1" dose not include the whole of the matched atom.

:g/*/s/\([0-9A-Za-z*\/,]* \)\{5}/\1 T /

it leaves the following after replacement.

*  T find /var/log/tomcat/* -mtime +61 -type f -delete

I am looking for

1 3 1 * * T find /var/log/tomcat/* -mtime +61 -type f -delete
share|improve this question
up vote 2 down vote accepted
:g/\*/s/\(\ \)\{5\}/T/

This doesn't work as you want, since it says "replace five consecutive spaces with T" to Vim.

This command should work as you need:


See :help /\v if you don't know what "very magic" mode is.

share|improve this answer
Thanks that work as expected. – nelaar Jul 31 '12 at 11:38
:g/*/s/^\v(\s*(\S+\s+){4}\S+)\s+(.*)/"\1";"\3"/ now produces what I am looking for. "1 3 1 * ";"find /var/log/tomcat/ -mtime +61 -type f -delete" – nelaar Jul 31 '12 at 11:58

The SedS command suggested by Andreas and Andy is well suited for this kind thing:

:line1,line2SedS/ \+/ T /5
share|improve this answer
puting a whole function into vim to simulate sed is a bit of over kill. But it works. – nelaar Jul 31 '12 at 12:06

This grep regex matches in the way I expect and replaces the way I want it to.


1 3 1 * * find /var/log/tomcat/* -mtime +61 -type f -delete

"1 3 1 * *";"find /var/log/tomcat/* -mtime +61 -type f -delete"

Thanks to xaizeks answer.

Using :bufdo in vim I can edit all the files I have open making the replacements I want and finally "|w" saving them. So all 10 copies I have of our servers cron jobs are now ready to pulled picked apart.

:bufdo g/\*/s/^\v(\s*(\S+\s+){4}\S+)\s+(.*)/"\1";"\3"/|w
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .