Home > Mobile >  Replace string in file based on regular expression match
Replace string in file based on regular expression match

Time:11-23

I have a file with a list of entries as below:

SERVER_ONE,19:05:07
SERVER_TWO,26,19:05:07

What I need to do is for the lines that only have one comma, I need to replace them with ,0,.

My regular expression captures what I need to replace correctly:

egrep "[A-Z],[0-9]{2}:" uptime.csv
SERVER_ONE,19:05:07

but I'm having some trouble implementing the replace strategy.

CodePudding user response:

Perl makes this easy. Try this command:

perl -pi -e 's/,/,0,/ if (m/^[^,]*,[^,]*$/)' uptime.csv
  • Related