Adding text to file on certain lines with(special characters)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding text to file on certain lines with(special characters)
# 8  
Old 12-15-2009
Code:
$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.

HTH,
PL
# 9  
Old 12-16-2009
Computer

Mumford,

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! Smilie 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
# 10  
Old 12-16-2009
Try this:

Code:
$
$ 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
$
$

tyler_durden
# 11  
Old 12-16-2009
Thx tyler.. That is what I ended up doing was substitution. but interesting thing was: s/$\RM/$\RM \$RM2/;

Finally it printed the dollar sign. Now im having the same issue with this:

Code:
$list[110] = \"/$HOME/etc/rm2_start"\"\n";
$list[210] = \"/$HOME/etc/rm2_stop"\"\n";

this is what I want to print:

Code:
"$HOME/etc/rm2_start"
"$HOME/etc/rm2_stop"


Last edited by Scott; 12-16-2009 at 01:11 PM.. Reason: Added code tags
# 12  
Old 12-16-2009
Quote:
Originally Posted by A4ron4perl
... im having the same issue with this:

Code:
$list[110] = \"/$HOME/etc/rm2_start"\"\n";
$list[210] = \"/$HOME/etc/rm2_stop"\"\n";

this is what I want to print:

Code:
"$HOME/etc/rm2_start"
"$HOME/etc/rm2_stop"

Put this in your code:

Code:
$list[110] = "\"/\$HOME/etc/rm2_start\"\n";
$list[210] = "\"/\$HOME/etc/rm2_stop\"\n";

Here's the testcase:

Code:
$ 
$ # 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 ----------

Quote:
Originally Posted by quirkasaurus
very nice job, tyler.
Thanks quirkasaurus. Appreciate it.

tyler_durden
# 13  
Old 12-16-2009
Hey tyler,

When I run the script I get a weird error message where these lines are suppose to be in the file:

Code:
$list[110] = "\"/\$HOME/etc/rm2_start\"\n";
$list[210] = "\"/\$HOME/etc/rm2_stop\"\n";

It prints on line 110 / 210 the following: SCALAR (0x6C1345) <--- something like this.
# 14  
Old 12-16-2009
Quote:
Originally Posted by A4ron4perl
...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.

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to enter special characters in a text file using vi?

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)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Search special characters in a file and replace with meaningful text messages like Hello

Search special characters in a file and replace with meaningful text messages like Hello (2 Replies)
Discussion started by: raka_rjit
2 Replies

3. Shell Programming and Scripting

Help with listing file name containing particular text and count of lines with 10 characters.

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)
Discussion started by: Gangadhar Reddy
2 Replies

4. Shell Programming and Scripting

Lines starting with special characters

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)
Discussion started by: magnus29
2 Replies

5. Shell Programming and Scripting

Fetch the lines which contains special characters

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)
Discussion started by: joe!!
3 Replies

6. Shell Programming and Scripting

grep lines having special characters

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)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

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)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

Remove special characters from text file

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)
Discussion started by: kkb
10 Replies

9. Shell Programming and Scripting

adding text to a file between lines

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)
Discussion started by: bishweshwar
7 Replies

10. Shell Programming and Scripting

adding lines at special place in crontab

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)
Discussion started by: Nicol
5 Replies
Login or Register to Ask a Question