Click here to Skip to main content
15,886,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to get expected output for an input file like below and output should be as below using any program awk/sed/perl/shell/python. please help

input:

abc
APPLE
123
456
789
def
APPLE
321
654
987
.
.
.

expected Output: append sequence before pattern (APPLE is pattern here) match

abc1
APPLE
123
456
789
def2
APPLE
321
654
987
.
.


What I have tried:

Nothing as of now, please help
Posted
Updated 17-Mar-21 2:48am
Comments
OriginalGriff 17-Mar-21 8:45am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And especially when your input and expected output examples are identical ...
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Try
Python
fin = open('input.txt', 'r')
fout = open('output.txt', 'w')

linesin = fin.read().splitlines()

seqnum = 1
previous = None
for lin in linesin:
  if previous != None:
    if lin == 'APPLE':
      previous += str(seqnum)
      seqnum = seqnum + 1
    fout.write(previous+'\n')
  previous = lin

fout.write(previous+'\n')
fout.close()
fin.close()
 
Share this answer
 
Comments
Santosh Kumar 2021 17-Mar-21 10:00am    
Thanks a lot CPallini. above solution solved my problem
CPallini 17-Mar-21 10:23am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900