I have ten files text1.html...text10.html. There are numbers 1234567890 in each file. How I can change 1234567890 with 0987654321 in each file from the terminal without opening files?

  • 1
    grep is for searching text, not changing them... you can use other tools like sed with -i option... – Sundeep Jun 13 '17 at 15:39
  • 2
    What do you mean by without opening files? Tools will have a hard time changing the content of a file it they can't even look into it or open it for writing. – Stéphane Chazelas Jun 13 '17 at 15:40
  • @StéphaneChazelas I mean find the files and change the text from the terminal via script. – Antranig Avakian Jun 13 '17 at 15:42
  • 1
    Do you mean that each file contains literally 1234567890, which you want to change to 0987654321? Or do you mean that each file contains a ten-digit number, like 6095178342, and you want to reverse it to (for example) 2438715906? Are the files all exactly one line long? If not, do you want every line processed the same way? Are there any lines that don’t have ten-digit numbers? E.g., are there any numbers with fewer or more than ten digits, and are there any lines with more than one number? Are there any lines with non-numeric data (e.g., letters)? – G-Man Says 'Reinstate Monica' Jun 13 '17 at 23:45
  • 1
    Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Jun 13 '17 at 23:45
for i in `seq 1 10`;
        sed -i 's/1234567890/0987654321/' text$i.html

If you're lazy, here it is in a for loop. ;)

  • 1
    As written that'll print out the substuted text without saving it anywhere. Perhaps you meant sed -i.bak 's/1234567890/0987654321/g' text{1..10}.html – roaima Jun 13 '17 at 19:32
  • 1
    @roaima So true! forgot the -i – AfroJoe Jun 13 '17 at 19:38
  • 2
    @AfroJoe why use cycle when you can feed sed file mask like in roaima's comment? – rush Jun 13 '17 at 20:15
  • 1
    @rush You totally can, and it IS the better solution. :) – AfroJoe Jun 14 '17 at 12:04

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.