2

Could somebody help me figure out a simple way of doing this using any script ? I will be running the script on Linux

1 ) I have a file1 which has the following lines :

 (Bank8GntR[3] | Bank8GntR[2] | Bank8GntR[1] | Bank8GntR[0] ),

 (Bank7GntR[3] | Bank7GntR[2] | Bank7GntR[1] | Bank7GntR[0] ),

 (Bank6GntR[3] | Bank6GntR[2] | Bank6GntR[1] | Bank6GntR[0] ),

 (Bank5GntR[3] | Bank5GntR[2] | Bank5GntR[1] | Bank5GntR[0] ),  

2 ) I need the contents of file1 to be modified as following and written to a file2

 (Bank15GntR[3] | Bank15GntR[2] | Bank15GntR[1] | Bank15GntR[0] ),

 (Bank14GntR[3] | Bank14GntR[2] | Bank14GntR[1] | Bank14GntR[0] ),

 (Bank13GntR[3] | Bank13GntR[2] | Bank13GntR[1] | Bank13GntR[0] ),

 (Bank12GntR[3] | Bank12GntR[2] | Bank12GntR[1] | Bank12GntR[0] ),

So I have to:

  1. read each line from the file1,
  2. use "search" using regular expression,
  3. to match Bank[0-9]GntR,
  4. replace \1 with "7 added to number matched",
  5. insert it back into the line,
  6. write the line into a new file.
MGwynne
  • 3,512
  • 1
  • 23
  • 35
Neel
  • 51
  • 1
  • 3
  • You haven't replaced it with "number matched multiplied 2", you've just added 7 to each. – MGwynne Jun 07 '11 at 18:53
  • sorry ! i just realized that and made the changes – Neel Jun 07 '11 at 18:56
  • Do you have to use python? Or can it be anything? Your tags mention `sed` and you stress the point that it's a script running on Linux, so I'm not sure on whether it *has* to be in Python. – MGwynne Jun 07 '11 at 19:29
  • 1
    actually it can be anything ... does not have to by python. – Neel Jun 07 '11 at 21:16

2 Answers2

3

How about something like this in Python:

# a function that adds 7 to a matched group.
# groups 1 and 2, we grabbed (Bank) to avoid catching the digits in brackets.
def plus7(matchobj):
  return '%s%d' % (matchobj.group(1), int(matchobj.group(2)) + 7)

# iterate over the input file, have access to the output file.
with open('in.txt') as fhi, open('out.txt', 'w') as fho:
  for line in fhi:
    fho.write(re.sub('(Bank)(\d+)', plus7, line))
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • Notes: `with` only supports one file handle prior to 2.7. You'd need to nest two statements if you're working with anything <= 2.6. – g.d.d.c Jun 07 '11 at 19:33
0

Assuming you don't have to use python, you can do this using awk:

cat test.txt | awk 'match($0, /Bank([0-9]+)GntR/, nums) { d=nums[1]+7; gsub(/Bank[0-9]+GntR\[/, "Bank" d "GntR["); print }'

This gives the desired output.

The point here is that match will match your data and allows capturing groups which you can use to extract out the number. As awk supports arithmetic, you can then add 7 within awk and then do a replacement on all the values in the rest of the line. Note, I've assumed all the values in the line have the same digit in them.

MGwynne
  • 3,512
  • 1
  • 23
  • 35
  • Wow ! this works like a charm. i could have never come up this with my rudimentary knowledge in awk. thanks a lot !! – Neel Jun 07 '11 at 21:20
  • I actually had to look up how to get awk to allow me to use capturing groups myself. I found the following comment - http://stackoverflow.com/questions/2957684/awk-access-captured-group-from-line-pattern/4673336#4673336 which was very helpful! – MGwynne Jun 07 '11 at 21:47
  • Thanks ! pretty useful link !! – Neel Jun 07 '11 at 22:07