0

I have a file to which I would like to add line numbers selectively. Say the target string begins with INIT "command" where command is a variable text string. I want it to be INIT "linenumber command". The idea is that each INIT line is tagged with a unique id ordered but not necessarily contiguous. That unique id could be its line number. Bonuses (virtually of course!) if the number could be in hex.

EDIT:

Sample input

#: ../src/app-utils/business-prefs.scm:24
msgid "Counters"
msgstr ""

Sample output

#: ../src/app-utils/business-prefs.scm:24
msgid "123f>Counters"
msgstr "123f>شمارنده‌ها"

The file is filled with thousands of these msg couples. The msgid and msgstr tags match thus have to have a same code. With the code below I would run it twice and change the inside parameter. I think I have to change some numbers in the code to adapt it for the sample. I have added ">" just to make it easier to recognize the code from the actual string i.e. some beautification.

3
  • 1
    As already mentioned, please provide your input and expected output as example Dec 21, 2016 at 22:51
  • Hey! Didn't receive notifications, I thought no response and went on to do editor regex search and hunt ;) but Almost. Well at least I can say belated merry Xmas and on-time happy new year!
    – Hamid
    Jan 1, 2017 at 2:19
  • Happy New Year ! :D Jan 1, 2017 at 6:45

1 Answer 1

1

Not sure if this is exactly what you want (you didn't provide the requestd examples yet), but here's an attempt to solve it with a small Python script. It reads data from standard input and prints them t standard output, inserting an index number in hexadecimal notation in each line that starts with "INIT ":

#! /usr/bin/env python3
import sys

i = 0 if len(sys.argv) == 1 else int(sys.argv[1])
while True:
    line = sys.stdin.readline()
    if not line:
        break
    if line.startswith("INIT "):
        print(line[:5] + hex(i)[2:] + line[4:], end="")
        i += 1
    else:
        print(line, end="")

I saved the script above as numberinit.py and made it executable using chmod +x numberinit.py.

You are supposed to run it in one of these two ways:

./numberinit.py < INPUTFILE
./numberinit.py STARTINDEX < INPUTFILE

INPUTFILE is the file you want to read and the optional STARTINDEX parameter lets you specify the first index to use, default is 0.

Usage example:

$ cat example.txt 
my input file
INIT first
INIT second
stuff in between
INIT third
INIT something longer as fourth

$ ./numberinit.py < example.txt 
my input file
INIT 0 first
INIT 1 second
stuff in between
INIT 2 third
INIT 3 something longer as fourth

$ ./numberinit.py 42 < example.txt 
my input file
INIT 2a first
INIT 2b second
stuff in between
INIT 2c third
INIT 2d something longer as fourth

You can of course redirect the output of the command to save it in a different file, but don't use the same input and output file (will truncate the file due to how your shell handles redirections):

./numberinit.py < input.txt > output.txt
2
  • One step in the right direction. I have updated the question with a sample but I think it works fine if I only change that INIT to something I should. But would you do a favor and make those length items parameter independent so that you only have to change INIT and no numbers?
    – Hamid
    Jan 1, 2017 at 2:23
  • That required some modification but worked! GOF (meaning good enough)
    – Hamid
    Jan 1, 2017 at 23:30

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.