0

I would like to place the small text fragments on the long sentence such as following

Input_full sentence

TRSTUVWSRTPSAREWSTRESW 

list of short fragments

TRST 
RESW

Output

I want the output such that wherever the short fragments match, it can be visible and placed exactly below the long sentence

TRSTUVWSRTPSAREWSTRESW 
||||              |||| 
TRST              RESW
2
  • This sounds very much like a homework assignment.
    – dobey
    Jan 17, 2017 at 15:27
  • I am not programmer, but in order to analyse my proteomics mass spectrometry data I needed such text processing
    – Trupti
    Jan 17, 2017 at 15:58

1 Answer 1

1

With some python:

#! /usr/bin/python3

import sys

textfile = 'input.txt'
patfile = 'patterns.txt'

with open(textfile) as text:
    with open(patfile) as pat:
        patterns = [line.strip() for line in pat]

        for line in text:
            line = line.strip()
            print(line)
            out = line[:]
            for p in patterns:
                out = out.replace(p, '|'*len(p))
            print(''.join(v if v == '|' else ' ' for v in out))
            print(''.join(line[k] if v == '|' else ' ' for k, v in enumerate(out)))

Essentially, this does, for each line in the input file:

  1. Prints the line, and takes a copy.
  2. For every pattern, replace the occurrences of the pattern in the line with | in the copy.
  3. Then it prints the copy with everything except | replaced with spaces.
  4. Then it prints the original line's character wherever | occurs in the copy, or spaces otherwise.

I assume that the patterns are fixed string which don't contain |.

2
  • This works fine, but whenever I have longer sentence it does not align completely (only half of its correctly aligned). Also I should have mentioned that my sentence does not contain any space.
    – Trupti
    Jan 18, 2017 at 14:42
  • @Trupti :shrug: unless you give a proper example, there's nothing I can do
    – muru
    Jan 18, 2017 at 14:45

Not the answer you're looking for? Browse other questions tagged .