I am confused over this, so need some help. I have multiple txt files in one folder. And there's some content in those text files. Now there is one text "moveLimit":30, (comma is after the decimal). So how do I add 100 to all the moves decimal in multiple files and save them. The moves is not 30, it's random number in every text files. And they all are between 10-999.


{"gameModeName":"Classic moves","moveLimit":106,"numberOfColours":6,"portals":[],"protocolVersion":"0.3","randomConfig":{"mode":"Seed Pool","modeModifier":"Tutorial Only","overrideAbTest":true,"seedPool":[547787]},"randomSeed":1088126,"scoreTargets":[300,400,500],"tileMap":[["000","000","000","000","000","000","000","000","000"],["000","000","000","000","000","000","000","000","000"],["002005","002005","002005","002005","002005","002005","002005","002005","000"],["002","002","002","002","002","002","002","002","000"],["002","002","002","002","002","002","002","002","000"],["002","002","002","002","002","002","002","002","000"],["002","002","002","002","002","002","002","002","000"],["000","000","000","000","000","000","000","000","000"],["000","000","000","000","000","000","000","000","000"]]}
  • 3
    post a testable file's content (we need to see whether "moves":30, is a part of the line or stands as a separate line) May 8 '18 at 7:12
  • It's in a single line, and doesn't stand therefore as a separate line.
    – PJ547
    May 8 '18 at 7:19
  • 1
    A full example would still be good.
    – Kusalananda
    May 8 '18 at 7:24
  • 2
    Instead of uploading to pastebin, please put a sample here in the question. AND a sample of the corresponding expected output Just 4 or 5 lines will be sufficient. (Paste it here, select the lines, use {} on the formatting bar to mark it as code.) A question should ideally be complete in itself.
    – roaima
    May 8 '18 at 7:36
  • 1
    Also specify whether there could be multiple entries "moves":<number>, on the same line May 8 '18 at 7:39

If you would say at the very start that your txt files are actually JSON files (as your sample just looks like) - it would have changed everything and prevent most of the questions/comments.

Use a proper JSON tools like jq as this would be the right way:

for f in *.txt; do
    if grep -q '"moveLimit":[0-9]*,' "$f"; then
        jq -c '.moveLimit |= . + 100' "$f" > tmp_f && mv tmp_f "$f"

On a GNU or FreeBSD system, you could try something like:

grep  -rl --null '"moveLimit":[0-9]' . |
  xargs -r0 perl -pi -e 's/"moveLimit":\K\d+/$& + 100/ge'
  • grep recursively lists the files in the current directory (.) that contain "moveLimit": followed by a decimal digit (null-delimited for xargs -0)
  • xargs -r0 passes that list of files to perl
  • perl edits the files in-place and replaces sequences of digits (\d+ that follow "moveLimit": with the same number ($&) plus 100.

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.