Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have two files, one with blocks like

<record>
line1
line2
line3
#model bla bla bla
</record>

while the other file has exactly the same number of blocks, but without the hashtag comments, ie like

<record>
line4
line5
line7
line8
line9
line10
</record>

So, what I want to do, is to copy from the 1st block of the first file the hashtag to the 1st block of the second file so that my other file also looks like

<record>
line4
line5
line7
line8
line9
line10
#model bla bla bla
</record>
share|improve this question
add comment

2 Answers

If you want it in the same order small code in python

import os
os.rename("second","second~")
a=[]
f=open("first","r")
lines=f.readlines()
b=-1
for line in lines:
    if "</record>" in str(line):
        if b!=-1:
            a.append(lines[b])
        else:
            print "</record> found in first line"
    b=b+1
f.close()
b=0
destination = open("second","w")
source = open("second"+"~","r")
for line in source:
    if "</record>" in str(line):
        destination.write(a[b])
        b=b+1
    destination.write(line)
source.close()
destination.close()

note:first is the file containing the hashtag and second is the file without hashtag

share|improve this answer
    
Hi - Thanks for this! Indeed it works, but the problem is that in the header of the first file, there are several other hastags that also include the #model - So, what I actually need is from each <record> to copy the line just before the closing block </record> - This would insure that I get the correct line - Would that be possible ? Thanks again! –  Alex Nov 30 '12 at 18:27
    
edited :) hope this helps –  harish.venkat Nov 30 '12 at 18:48
    
Sweet!!!! thanks bunch for your precious help! It works..almost perfect, except the fact that is copies the #hastag from the very first <record> block to all blocks of the target file..It should copy from each <record> block to the corresponding block in the target... Sorry if was not clear... –  Alex Nov 30 '12 at 18:53
    
dude i corrected it now sorry i by mistake left b=b+1 there, third line from last –  harish.venkat Nov 30 '12 at 18:57
    
sorry...not working Traceback (most recent call last): File "hashtag.py", line 20, in <module> destination.write(a[b]) IndexError: list index out of range –  Alex Nov 30 '12 at 19:01
show 2 more comments

From your sample, it looks like this simple awk command should be enough:

awk '/<\/record/{"grep \\^\\# file1"|getline a; print a};1' file2

Or, if as figured out from the further requirements exposed in the comments to the other answer which you forgot to add to your question ;-), hash tags may appear outside <record> blocks:

sed '/<record>/,/<\/record>/!d;/^#/!d' < file1 |
  awk '/<\/record/{getline a < "-"; print a};1' file2
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.