1

I have a file which contains data like this:

$ yum -- to install package
admin1,group,n,0123456,/usr/bin 
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin
----->#i have to insert a new line here
$cat -- to read contents of a file
admin1,group,n,0123456,/usr/bin
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin

First, I have to find the the word $ yum in the file and insert a new line before starting of the next word starting with $.

Some help with my code would be appreciated.

 with open("test.txt", "a") as myfile:
      for row in myfile:
            if re.match(r'$yum,i): 
                myfile.append("user2,group,0123456,/usr/bin") 
7
  • Is the file the input and the file with a new line the output? Commented Sep 6, 2015 at 12:01
  • 1
    Have you tried anything yet? Post your code so that we can help you with any problems that you have. Commented Sep 6, 2015 at 12:03
  • 1
    you don't need a regex Commented Sep 6, 2015 at 12:09
  • no that is the complete file, i have to insert a new line at "---> " this indicates where new line has to be entered. Commented Sep 6, 2015 at 12:15
  • with open ("fileone","a") as file: Commented Sep 6, 2015 at 12:17

2 Answers 2

1

Opening the file with a does not allow you to file_object.append(..., it means when you write to the file content will be added to the end of the file so myfile.append("user2,group,0123456,/usr/bin") is not remotely valid syntax.

You can use itertools.groupby to group by section where lines start with a $, then either add a line if the line starts with $ yum or just write the lines as is:

from itertools import groupby
from tempfile import NamedTemporaryFile
from shutil import move

with open("in.txt") as f, NamedTemporaryFile("w",dir=".",delete=False) as out:
    grps = groupby(f,key=lambda x: x.startswith("$"))
    for k, v in grps:
        if k:
            val = next(v)
            out.write(val)
            if val.startswith("$ yum"):
                out.writelines(next(grps,[[], [""]])[1])
                out.write("I am a new line\n")
        else:
            out.writelines(v)
move(out.name,"in.txt")

The move(out.name,"in.txt") will change the original file content so the output will be:

$ yum -- to install package
admin1,group,n,0123456,/usr/bin 
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin
I am a new line
$cat -- to read contents of a file
admin1,group,n,0123456,/usr/bin
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin

Os use an inner loop every time you find a line starting with $ yum and breaking and writing the new line in the inner loop whenever you find the next $:

from tempfile import NamedTemporaryFile
from shutil import move

with open("in.txt") as f, NamedTemporaryFile("w",dir=".", delete=False) as out:
    for line in f:
        if line.startswith("$ yum"):
            out.write(line)
            for _line in f:
                if _line.startswith("$"):
                    out.write("I am a new line\n")
                    out.write(_line)
                    break
                out.write(_line)
        else:
            out.write(line)

move(out.name,"in.txt")
Sign up to request clarification or add additional context in comments.

Comments

0

Using fileinput.input() you can easily and safely overwrite the input file without explicitly using a secondary file and then overwriting the original file:

import fileinput

extra_line = 'new line here...'
seen_yum = False
f = fileinput.input('test.txt', inplace=True)
for line in f:
    if line.startswith('$ yum'):
        seen_yum = True
    elif line.startswith('$') and seen_yum:
        print extra_line
    print line,
f.close()

For the input:

some other lines
of no interest to us
$ yum -- to install package
admin1,group,n,0123456,/usr/bin
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin
$cat -- to read contents of a file
admin1,group,n,0123456,/usr/bin
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin

the output would be:

some other lines
of no interest to us
$ yum -- to install package
admin1,group,n,0123456,/usr/bin 
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin
new line here...
$cat -- to read contents of a file
admin1,group,n,0123456,/usr/bin
user2,group,n,0123456,/usr/bin
group,n,0123456,/usr/bin

It's not clear from your question whether the extra line should be inserted before every subsequent line that starts with $ - as the code above does. If you only want one extra line inserted for every $ yum seen you can reset the seen_yum flag.

import fileinput

extra_line = 'new line here...'
seen_yum = False
f = fileinput.input('test.txt', inplace=True)
for line in f:
    if line.startswith('$ yum'):
        seen_yum = True
    elif line.startswith('$') and seen_yum:
        seen_yum = False
        print extra_line
    print line,
f.close()

So if the file contains multiple lines that start with $ yum, and there is a following line that begins with $, a new line will be inserted.

6 Comments

@mkhawke if $yum is found , we have to insert one new line or couple of line depending upon the requirement given to us . those lines has to be low $yum but above the line $(some other command )...basically i have a file given to me with same as present in server with some diff. i have to add those lines which are not there in old files in specific order.
Other than inserting multiple lines (if I understand that requirement) which is a trivial adaption of this code, how does this answer not satisfy that requirement?
@mkhawke , are we modifying the file also with the prinit statement ,? will the file be appended at the desire place? with the above code?
@Alekh: have you tried running the code? Yes, the file will be modified by using a print statement, and the new lines inserted at the required position. fileinput.input() works by redirecting sys.stdout to the file, so a normal print statement will write the file.
@mkhwke the above code writes the new line above the $ yum but i want that to be written below. please help..
|

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.