Sponsored Content
Top Forums Shell Programming and Scripting Adding text to file on certain lines with(special characters) Post 302380896 by durden_tyler on Wednesday 16th of December 2009 02:41:49 PM
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
 

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 05:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy