I need to write a sed command that reads from a file and replaces the first character in each line with 0, only in the event that the first and second character in that line is a digit (and leave the rest of the lines intact).


You can find lines that begin with two digits using either ^[0-9][0-9] or ^[0-9]\{2\}

Then you can replace a single character with 0 using s/./0/

Putting it together,

sed '/^[0-9][0-9]/ s/./0/' somefile


sed '/^[0-9]\{2\}/ s/./0/' somefile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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