Perl : how to modify a file without generate a temporary file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : how to modify a file without generate a temporary file
# 1  
Old 08-03-2010
Perl : how to modify a file without generate a temporary file

Hi All,

I have a file like below, how can i insert one line after line 1 without using a temporary file in perl?

Code:
line 1
line 2
line 3

expected result
Code:
line 1
new line <---insert here
line 2
line 3

# 2  
Old 08-03-2010
Hi

Code:
[root@gpr ~]# cat a
abc
efg
hij
[root@gpr ~]# perl -i -pne  'if($.==2){print "more\n"}' a
[root@gpr ~]# cat a
abc
more
efg
hij
[root@gpr ~]#

Guru.
# 3  
Old 08-03-2010
thanks, but still more help needed

thanks very much for your reply.
with inplace file edit, it is ok for this easy case.
but my requirement is a little bit complicated:

1> rather than directly insert lines in original file, still need to open file to read and check some key word

2> if key words found, print the key words

3> if key words not found, insert into file

example

original file
Code:
[books]
java = 30$
oracle = 20$
[tools]
Ultraedit = 100$
Eclipse = 200$


supposed the perl script is named as conf.pl, -s option is for section, -k option is for key, -v option is for value

Code:
1> conf.pl -s books -k java
    print 30$

2> conf.pl -s books -k perl -v 40$
    append line 
    perl = 40$ 
    after line 
    oracle = 20$

3> conf.pl -s cds -k english -v 100$
    append 
    [cds]
    english = 100$


So basically the case is modify file content on hot at any time if meet or not-meet any condition.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Programming

Storing a Temporary File Using C

How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it using unlink? #include <stdio.h> #include "error.h" ... (15 Replies)
Discussion started by: metallica1973
15 Replies

3. UNIX for Dummies Questions & Answers

Mailx - temporary mail message file: No such file or directory

How would I go about resolving this error temporary mail message file: No such file or directory Can anybody tell me where the default location is for the temporary mail message file is for mailx? It appears that it doesn't exist. Thanks (1 Reply)
Discussion started by: joen
1 Replies

4. Shell Programming and Scripting

Perl script to modify csv file

Hi Friends, I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file table findhost= { {"xyz","abc"}, {"rxz","mmz"}, {"vrr","nnz"}, } default={"NONE"} My current perl script #!/usr/bin/env perl... (12 Replies)
Discussion started by: dbashyam
12 Replies

5. Shell Programming and Scripting

Modify the file with awk,sed or perl

Hi All, I need help from any of you.Would be so thankful for your help. I/P DDDD,1045,161,1557,429,1694,800,1911,1113,2460,1457,2917> 1609,3113,1869,3317,2732,3701,3727,4132,5857,5107> 9004,6496 DDDD,1125,157,1558,429,1694,800,1911,1117,2432,1444,2906>... (2 Replies)
Discussion started by: Indra2011
2 Replies

6. Shell Programming and Scripting

How to remove the first line of a file without using any temporary file

I want remove first line of the file without using any temporary file. Everything should be done in the original file itself since I don't any file creation access whereas I have modify access. It would be great if somebody help in this regard ASAP . Thanks in Advance. Kishore:mad: (1 Reply)
Discussion started by: tvbhkishore
1 Replies

7. Shell Programming and Scripting

temporary file to file then move to directory

Ok in my bash script i have 5 options to create a simple html script. I need to create a temporary file and whatever the user types will be stored in that file using html codes. And then I have another option in which that temporary file will be moved to the public_html directory in which the user... (19 Replies)
Discussion started by: gangsta
19 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

remove temporary file ?

Hi all, In the script I am creating a temporary file with process id as temp.txt.$$ I want to remove this tomporary file first from the current directory when i'll run the same script next time. Note: Every time when the script executes then it has unique process id and it'll create a... (5 Replies)
Discussion started by: varungupta
5 Replies

10. Shell Programming and Scripting

Modify script to generate a log file

I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question