Hi All,
I have a file like below, how can i insert one line after line 1 without using a temporary file in perl?
line 1
line 2
line 3
expected result
line 1
new line <---insert here
line 2
line 3
Hi All,
I have a file like below, how can i insert one line after line 1 without using a temporary file in perl?
line 1
line 2
line 3
expected result
line 1
new line <---insert here
line 2
line 3
Hi
[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.
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
[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
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.