Sponsored Content
Top Forums Shell Programming and Scripting Adding text to file on certain lines with(special characters) Post 302380864 by durden_tyler on Wednesday 16th of December 2009 11:37:04 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy