-4

Is there any windows or unix/linux commandline script that can do this?I.e the word "boss" should be incremented from 1 to 60,then 1 to 60 again.

I have a file that contains a list of text like so;

She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
.
.
.

I want the output to be:

She is the new boss1
She is the new boss2
She is the new boss3
.
.
.
She is the new boss60
She is the new boss1
She is the new boss2
She is the new boss3
.
.
.
She is the new boss60
.
.
.

So,far I use this

perl -i.bak -ape "s/"old-text"/$&.++$A /ge" input.txt && del input.txt.bak
to do repeated increment but the output it produces is not what I want.
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • This might help you. https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash – WyattBlue Jun 28 '20 at 04:46
  • 2
    Welcome to Stack Overflow. SO is a question and answer page for professional and enthusiastic programmers. Please add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. – Cyrus Jun 28 '20 at 04:49

5 Answers5

2
perl -pe 's/\bboss\b\K/(++$n > 60 ? $n=1 : $n)/eg' file

Word boundaries \b have been added around boss to prevent matching bossy or embossed.


Originally, you used $&.++$A:

It's easier to not use the save-in-place -i option until you know that the processing being applied is correct.

1
perl -ple'$_ .= ($. - 1) % 60 + 1'

$. is the current line number.

Specifying file to process to Perl one-liner

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

This Linux command may do:

i=1 && while read f; do echo "$f$i"; i=$(( i+1 )); done <original_file.txt > new_file.txt
0

Here is an awk:

$ echo "She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss" | awk -v max=3 '{cnt>=max ? cnt=1:cnt++; $NF=$NF cnt;} 1'

Prints:

She is the new boss1
She is the new boss2
She is the new boss3
She is the new boss1
She is the new boss2
She is the new boss3
She is the new boss1
She is the new boss2

So just change the input max to 60 and that should work as your example describes.


For a file, just do:

$ awk -v max=60 '{cnt>=max ? cnt=1:cnt++; $NF=$NF cnt;} 1' your_file
dawg
  • 98,345
  • 23
  • 131
  • 206
0

I don't think python is clean in this task. But I write this for you.

import re


class LineReplacer(object):
    def __init__(self, replace_word, max_word_count):
        self.word_index = 1
        self.word_to_replace = replace_word
        self.max_word_count = max_word_count

    @property
    def word_regex(self):
        return re.compile(f"(?<=[^a-zA-Z0-9]){self.word_to_replace}(?=[^a-zA-Z0-9])")

    def replace_function(self, match):
        current_index = self.word_index
        if self.word_index < self.max_word_count:
            self.word_index += 1
        else:
            self.word_index = 1
        return f"{match.group()}{current_index}"

    def replace_file(self, input_file_path, output_file_path):
        output = open(output_file_path, "w")
        with open(input_file_path, "r") as f:
            for line in f.readlines():
                new_line = self.word_regex.sub(self.replace_function, line)
                output.write(new_line)
        output.close()


if __name__ == "__main__":
    replacer = LineReplacer("boss", 60)
    replacer.replace_file("test.txt", "output.txt")

This will generate a new file.

Xu Qiushi
  • 1,111
  • 1
  • 5
  • 10