I would love to use a one-liner from the command line. This is going into a larger script. This is my first script with perl and I am not a programmer. Opening a file is still awkward to me vs. something you are so used to it doesn't even register anymore.
What i would like to accomplish. Open a file. Write to file without overwriting everything or anything. If you had "vi" editing a file using :set number. I want to edit lines 31,39,40 etc without overwriting whats on those lines yet add new line with text.
Here is what I have that does not work Does what I want to accomplish make sense and am I close?
#!/usr/bin/perl
$file="/opt/control/etc/status";
open(FH, ">>$file")
or die "Can't open file: $!";
while (<FH>) {
$count++; }
if ($count==31) {
print FH qq[RM="rm2"];}
if ($count==40) {
print FH qq[$RM]
}
if ($count==43) {
print FH qq[rmDESC="Result Manager2"\n];
}
close FH;
exit;
In-place editing of a file isn't easy, even for perl pros. You should either write to a new file, then replace the old file with the new one when done, or load the entire file into a list (array), make your changes, then rewrite it.
Here's the first. There are much better ways to do some of these steps (there are modules for generating temporary names, for example), but I don't want to make it too complex:
And here's how with a list:
Notice how the list elements are 1 less than the lines you wanted to modify. The first element of a list is index 0.
The list solution is shorter, but it's a bad idea to do it this way if the status file is large since it loads the whole file into memory.
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)
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 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)
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,
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)
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 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,
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 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)