$file="abc.txt";
open(FH, "<$file") or die "Can't open file: $!";
my $tmp_file ="/tmp/abc.txt";
open (WFH,">$tmp_file") or die "$!";
while (my $line = <FH>) {
chomp $line;
$count++;
print WFH "[RM=\"rm2\"]\n" if ($count==31) ;
print WFH "[$RM]\n" if ($count==40);
print WFH "[rmDESC=\"Result Manager2\"\n" if ($count==43);
print WFH "$line\n";
}
close WFH;
close FH;
system ("mv $tmp_file $file");
Note i am not sure if the code which you gave works as intended as i see the while loop is just doing a count++ only and the remaining commands are outside the loop
If the file is too big this would be an ideal way to do as you are not loading any thing in memory alternatively if the file is very small then you can load the whole file into memory and then access it . In the latter case you will not be required to do a move.
I am having some success with your list method. Only having one issue which I can explain here in a minute. The files needing this sort of operation are not really that big considering the box has 48 gigs of memory. Files might have 100 or less lines of code that need edits. I am curious what an example of writing the new file, deleting the old one(replacing) would look like. Sounds simple enough. Here is my issue:
Code:
#!/usr/bin/perl
$file = "/opt/ontrol/etc/tatus";
$RM = "BLAH";
open($fh, "<", $file) or die "Could not open $file: $!\n";
@list = <$fh>;
close($fh);
$list[30] = "RM=\"rm2\"\n";
$list[37] = "$RM2";
$list[42] = "rm2DESC=\"Result Manager2\"\n";
open($fh, ">", $file) or die "Could not open $file: $!\n";
print $fh @list;
close($fh);
I have to edit the file names a little for SBU(sensitive but unclassified). Ok! List [37] = "$RM2"; the actual line on 37 is: Yadda="$RWHOIS $LM $RM"
I need $RM2 somewhere within the double quotations. Can't seem to print this one but everything else goes into the file.. Of course $RM2 is the most important its the PID. I could get by without a description showing correctly but the PID no way! Mumford your the man no matter what others on the forum say about you. Jk
Last edited by Scott; 12-16-2009 at 01:10 PM..
Reason: Please use code tags
$
$ cat change_status.pl
#!/usr/bin/perl
$file = "status";
$RM2 = "BLAH";
open($fh, "<", $file) or die "Could not open $file: $!\n";
@list = <$fh>;
close($fh);
$list[30] = "RM=\"rm2\"\n"; # this will affect line 31
$list[37] =~ s/"$/ $RM2"/; # this will affect line 38
$list[42] = "rm2DESC=\"Result Manager2\"\n"; # this will affect line 43
open($fh, ">", $file) or die "Could not open $file: $!\n";
print $fh @list;
close($fh);
$
$
Testcase below:
Code:
$
$ cat -n status
1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 This is line 14
15 This is line 15
16 This is line 16
17 This is line 17
18 This is line 18
19 This is line 19
20 This is line 20
21 This is line 21
22 This is line 22
23 This is line 23
24 This is line 24
25 This is line 25
26 This is line 26
27 This is line 27
28 This is line 28
29 This is line 29
30 This is line 30
31 This is line 31
32 This is line 32
33 This is line 33
34 This is line 34
35 This is line 35
36 This is line 36
37 This is line 37
38 Yadda="$RWHOIS $LM $RM"
39 This is line 39
40 This is line 40
41 This is line 41
42 This is line 42
43 This is line 43
44 This is line 44
45 This is line 45
46 This is line 46
47 This is line 47
48 This is line 48
49 This is line 49
50 This is line 50
$
$ cat -n change_status.pl
1 #!/usr/bin/perl
2
3 $file = "status";
4 $RM2 = "BLAH";
5
6 open($fh, "<", $file) or die "Could not open $file: $!\n";
7 @list = <$fh>;
8 close($fh);
9
10 $list[30] = "RM=\"rm2\"\n"; # this will affect line 31
11 $list[37] =~ s/"$/ $RM2"/; # this will affect line 38
12 $list[42] = "rm2DESC=\"Result Manager2\"\n"; # this will affect line 43
13
14 open($fh, ">", $file) or die "Could not open $file: $!\n";
15 print $fh @list;
16 close($fh);
$
$ perl change_status.pl
$
$ cat -n status
1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 This is line 14
15 This is line 15
16 This is line 16
17 This is line 17
18 This is line 18
19 This is line 19
20 This is line 20
21 This is line 21
22 This is line 22
23 This is line 23
24 This is line 24
25 This is line 25
26 This is line 26
27 This is line 27
28 This is line 28
29 This is line 29
30 This is line 30
31 RM="rm2"
32 This is line 32
33 This is line 33
34 This is line 34
35 This is line 35
36 This is line 36
37 This is line 37
38 Yadda="$RWHOIS $LM $RM BLAH"
39 This is line 39
40 This is line 40
41 This is line 41
42 This is line 42
43 rm2DESC="Result Manager2"
44 This is line 44
45 This is line 45
46 This is line 46
47 This is line 47
48 This is line 48
49 This is line 49
50 This is line 50
$
$
$
$ # show what lines [109..113] and [209..213] look like before script is run
$ perl -lne 'print $.,"\t",$_ if 109..113 or 209..213' status
109 This is line 109
110 This is line 110
111 This is line 111
112 This is line 112
113 This is line 113
209 This is line 209
210 This is line 210
211 This is line 211
212 This is line 212
213 This is line 213
$
$ # show the modified Perl script
$ cat -n change_status.pl
1 #!/usr/bin/perl
2
3 $file = "status";
4 $RM2 = "BLAH";
5
6 open($fh, "<", $file) or die "Could not open $file: $!\n";
7 @list = <$fh>;
8 close($fh);
9
10 $list[30] = "RM=\"rm2\"\n"; # this will affect line 31
11 $list[37] =~ s/"$/ $RM2"/; # this will affect line 38
12 $list[42] = "rm2DESC=\"Result Manager2\"\n"; # this will affect line 43
13 $list[110] = "\"/\$HOME/etc/rm2_start\"\n"; # this will affect line 111
14 $list[210] = "\"/\$HOME/etc/rm2_stop\"\n"; # this will affect line 211
15
16 open($fh, ">", $file) or die "Could not open $file: $!\n";
17 print $fh @list;
18 close($fh);
19
$
$ # run the script
$ perl change_status.pl
$
$ # show what lines [109..113] and [209..213] look like after script is run
$ perl -lne 'print $.,"\t",$_ if 109..113 or 209..213' status
109 This is line 109
110 This is line 110
111 "/$HOME/etc/rm2_start"
112 This is line 112
113 This is line 113
209 This is line 209
210 This is line 210
211 "/$HOME/etc/rm2_stop"
212 This is line 212
213 This is line 213
$
$
HTH,
tyler_durden
---------- Post updated at 02:41 PM ---------- Previous update was at 02:22 PM ----------
...It prints on line 110 / 210 the following: SCALAR (0x6C1345) <--- something like this.
I don't know what your program looks like now, so I shall explain that "SCALAR(0x6C1345)" thingy. My hunch is that you tried to assign a scalar variable to another with the backslash ("\") prefixed to it and forgot the double quotes.
The "SCALAR(0x6C1345)" is not an error message. It's the type of value pointed to by a reference, and its memory address. If you are familiar with the concept of a pointer in C, then you will be delighted to note that a reference is its (loose) equivalent in Perl.
Given below is a short one-liner where I create a Perl scalar variable $x, then create a reference to it, and then print the reference:
Code:
$
$ perl -le '$x = "hello, world!";
> $y = \$x; # $y now "refers" to (points to) $x; note that double-quotes are absent
> print $y; # oops, I am trying to print a reference
> '
SCALAR(0x916718c)
$
$
I hope the example above gives you an idea of what might be wrong in your code. You'll have to walk through it and figure out where you are doing something similar i.e. trying to print a reference, rather than a scalar. Otherwise, if you post your program here then we can have a look at it and try to find it for you.
Hi,
I need to create a test text file with the special characters \342\200\223 in it and to be able to use sed maybe to delete them
I tried doing it using vi by pressing CTRL-V and then typing 342 but it does not work. After pressing CTRL-V and typing 342 it seems to just insert the numbers... (1 Reply)
Hi,
I've 2 queries.
I need to list files which doesn't contain a particular text in the content. For example say, I need to list files which doesn't contain string "abc" from all files ending with *.bad. How can I do that?
Also, I want to display number of lines in a file which has atleast... (2 Replies)
Hi
I have a file and need to extract lines starting with "grep ^"
I tried with quotes single/double before/after but no luck.
suggestion pls, thanks! (2 Replies)
Hi All,
My source file contains special characters(Latin characters).I need to fetch only the lines which contains the special characters. The problem is i don't know which all latin/special characters can come in the source.
Is there anyway to extract the lines which contain letters other... (3 Replies)
Hi,
I have a file which has numerous lines and some of the lines having special characters in it. i want to grep the lines which are having special characters.
say,
one line looks like - %*()$#@"", | acbd
antoher line looks like ***##^%! | efcg
so these kind of lines are present... (5 Replies)
I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it.
For example, Here is a portion of a zone file.
IN NS ns1.domain.tld.
IN NS ns2.domain.tld.
IN ... (2 Replies)
Hi All,
i am trying to remove all special charecters().,/\~!@#%^$*&^_- and others from a tab delimited file.
I am using the following code.
while read LINE
do
echo $LINE | tr -d '=;:`"<>,./?!@#$%^&(){}'|tr -d "-"|tr -d "'" | tr -d "_"
done < trial.txt > output.txt
Problem
... (10 Replies)
Suppose content of my first file:
first line
second line
third line
How can i insert text between "first line" & "second Iline"
Any help?????/ (7 Replies)
Hi ,
i export the crontab in a file (i've no root right) and i would add lines from a file at a special place and rewrite the output in an another file.
the special place is as this :
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
so i would search for toto.sh and insert the lines , the... (5 Replies)