0

I have 3000 words that are repeated (ARG ARG ARG ARG ....)

I am trying to search and replace the 500th word by 'UIO'

I have been trying it with sed -i 's/ARG/UIO/500' filename.txt

I have also tried it with sed -i 's/ARG/UIO/500g' filename.txt but it does not work.

I does not report any mistake but it doesn't change anything in filename.txt either.

What would you recommend me to do?

4
  • 2
    Are the words all on the same line? Sed restarts its count every time it gets a newline. Commented Feb 27, 2017 at 21:10
  • 4
    I just tested this by creating a file with 3000 repetitions of ARG and sed 's/ARG/UIO/500' worked exactly as expected. So something is different in your case. Please post an example of your input file that reproduces the problem. Commented Feb 27, 2017 at 21:19
  • Add the -z option to make it do it on the 500th occurrence in the file as opposed to the 500th occurrence of each line (assuming the file doesn't contain NUL characters and a recent enough version of GNU sed). Commented Feb 27, 2017 at 22:09
  • The flag is working exactly as intended, as @JosephSible implies. Suggest using Perl in paragraph mode. Commented Feb 27, 2017 at 22:30

1 Answer 1

0

If the words are distributed over lines you could do (as portable solution)

sed -i 'H;1h;$!d;g;s/ARG/UIO/500' filename.txt

to collect lines in the hold space first and finally do the replacement with all lines in the same buffer.

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.