1

input:

dummy - 22847847
sha - 84734747
DHS - 374874

expected output:

du1my - 22847847
sh1 - 84734747
DH1 - 374874
  • 3
    The example does not change every 3rd character, it changes only the 3rd character (on each line). – Kusalananda Jul 31 '18 at 8:00
3
$ cat test.txt
dummy - 22847847
sha - 84734747
DHS - 374874

$ awk '{$3=1}1' FS="" OFS="" test.txt
du1my - 22847847
sh1 - 84734747
DH1 - 374874
3

With sed:

sed 's/^\(..\)./\11/' infile > expected_result

The (..) is known as a group match and it capture first two characters and the \1 is its back-reference. dot . in regular expression matches any single character.

if that was not 3rd place but Nth, you could do as following to change the 3rd place character with 1.

sed 's/^\(.\{2\}\)./\11/' infile

Or just like below as pointed by @steeldriver:

sed 's/./1/3' infile
  • 3
    Why not just s/./1/3? – steeldriver Jul 31 '18 at 7:59
  • @steeldriver I totally forgot that : ). – αғsнιη Jul 31 '18 at 8:46

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.