1

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).

3
  • No, I don't kbow where to start. Any help appreciated Commented Jun 4, 2016 at 10:52
  • have a look at manual man sed or info sed and tutorials like grymoire.com/Unix/sed.html and code.snipcademy.com/tutorials/shell-scripting/sed/introduction Commented Jun 4, 2016 at 11:19
  • 1
    A more meaningful title (to help others, in the future) might be something like "Replace only the first character of a matched pattern" Commented Jun 4, 2016 at 12:34

1 Answer 1

4

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

or

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.