1

I have data in the following format:

   This is line1 <line 1>.
   This is line2 <http://<line2> .
   This is line3 <http://<>line3>.
   This is line4 <line4> .

I want to transform this data into the following format:

   #@ <line 1>
   This is line1.
   #@ <http://<line2>
   This is line2.
   #@ <<http://<>line3> 
   This is line3. 
   #@ <line4>
   This is line4.

I tried in python by splitting at < but it does not solve my purpose as '<' and '>' are present within strings themselves. Is there any way in python or linux(sed, etc) by which I may achieve the above given transformation

2 Answers 2

3

Split only at the first <:

with open('foo.txt') as f:
    for line in f:
        a, b = line.split('<', 1)
        b = '#@ <' + b.rstrip('. \n')
        print b
        print a.rstrip() + '.'

Output:

#@ <line 1>
This is line1.
#@ <http://<line2>
This is line2.
#@ <http://<>line3>
This is line3.
#@ <line4>
This is line4.
Sign up to request clarification or add additional context in comments.

1 Comment

I have changed the input of question. My apologies I think that the input is somewhat misleading
1
sed 's/\(.*\)\(<line[0-9]\{1,\}>\)./#@ \2\
\1./' YourFile

1 Comment

It is not necessary that my line begins with "This is". Sorry for my mistake in the question..i have modified it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.