PYTHON COPY Contents of file1 into a specific entry in file2


 
Thread Tools Search this Thread
Top Forums Programming PYTHON COPY Contents of file1 into a specific entry in file2
# 1  
Old 04-14-2015
PYTHON COPY Contents of file1 into a specific entry in file2

file1
Code:
cat
dog
fish

file2
Code:
This is a bunch of lines
<!-- INSERT ANIMALS  HERE -->
horse
cheetah


post results
file2
Code:
This is a bunch of lines
<!-- INSERT ANIMALS  HERE -->
cat
dog
fish
horse
cheetah


code so far


Code:
import re
regex = re.compile(r"<[^*]-- INSERT ANIMALS HERE -->")
line_found = False

Code:
with open('file1.xml') as file1:
    for line in file1:
        if re.match(regex, line):
            line_found = True
            break
    with open('../file2.xml', 'a') as file2:
        if line_found:
              for line in file1:
                file2.write(line)


does not appear to be working at all
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code samples.

Last edited by Don Cragun; 04-14-2015 at 03:20 PM.. Reason: Add CODE tags.
# 2  
Old 05-25-2015
RedHat

Code:
$vi ab.py
prev_line_has_tag=False

with open('file2.xml','r') as f1:
        for line in f1:
                print("",line,sep='',end="")
                if line.startswith("<!--"):
                        prev_line_has_tag = True
                if prev_line_has_tag is True:
                        with open('file1.xml','r') as f2:
                                print("",f2.read(),sep='',end="")
                        prev_line_has_tag = False

output
Code:
$ python3 ab.py
This is a bunch of lines
<!-- INSERT ANIMALS  HERE -->
cat
dog
fish
horse
cheetah

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. UNIX for Dummies Questions & Answers

Copy(append ) the contents of file1 and file2 to file3

Platform : Oracle linux 6.5 I have two log files with the following contents # ls -l total 8 -rw-r--r--. 1 root root 75 Dec 10 20:55 myLogfile1.log -rw-r--r--. 1 root root 51 Dec 10 20:57 myLogfile2.log # # cat myLogfile1.log hello world jaded zombies acted quaintly but kept driving... (9 Replies)
Discussion started by: kraljic
9 Replies

4. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

5. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

8. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

9. Shell Programming and Scripting

grep -f file1 file2

Wat does this command do? fileA is a subset of fileB..now, i need to find the lines in fileB that are not in fileA...i.e fileA - fileB. diff fileA fileB gives the ouput but the format looks no good.... I just need the contents alone not the line num etc. (7 Replies)
Discussion started by: vijay_0209
7 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question