I've been tooling about with Perl to make a few string replacements in some files; and seem to have run into a bit of a squeeze
Beginning with a simple text file, test.txt, we have the following content to be worked:
Quote:
Mary had a little lamb.
Now, not wanting to have anyone feel left out, I decided that it would be nice if Joe could have his time in the sun as well. So, I invoked Perl from the commandline with the following generally-familiar "one liner":
Works like a charm: Quick and easy; and Joe's happy, too
Now, being a curious sort of fellow, I decided to "simply" create a script which'd do the same thing as the above command; leading to the following "best effort" with my limited knowledge of the topic:
Input text: "Mary had a little lamb."
Output: "test.txt"
I know there's a simple twist I'm missing here; but can't get a grip on it for the life of me
Now, if I want to be really square, I can simply do a bash script with the single line,
...and, of course, all works a treat without a fuss.
Incidentally, I will need to do a find/replace operation with whatever snippet finally emerges on several files at a time. I can get this to work as:
...but haven't a single clue as to how this would translate over to an actual script in Perl.
Any hints/tips to get my humble codebit going in the right direction???
Fixed:
While this ran, test.txt was unchanged; and the altered text was simply directed to the command line. Couldn't get any modification to work as planned...
So, after more digging around, I tried to rectify the problem thusly:
This block runs without warnings or errors, but instead of the text ($content) being altered and returned to the original file (as the apparent logic/flow would indicate), all in "test.txt" is erased; and the script simply exits.
FWIW, the perldocs were rather confusing/obtuse on all of this...
???
Thanks again
---------- Post updated at 04:20 PM ---------- Previous update was at 04:05 PM ----------
Stop the presses!
Working with something over here. Seems I got confused over handles/content.
Back soon --
---------- Post updated at 04:39 PM ---------- Previous update was at 04:20 PM ----------
Back again.
This time, the code seems better in some regards, but the result is still the same: All content of test.txt is simply erased--
Really confused now...
Now, just one more question from the noob: Is this "properly formed" Perl? Also, can one invoke $^I like this:
to avoid creating an intermediate temp file on the disk (could be useful when doing replacements on largish files)???
Finally, if you have just one more moment to spare along the way, what's the rule for quoting "string" and 'string' in a context such as this:
Both single and double quotes seem to work interchangeably without error or warning: Is the code agnostic when it comes to such conventions???
Thanks again!
Last edited by LinQ; 05-19-2014 at 02:04 PM..
Reason: Couple more thoughts ;)
Yes, that's the equivalent to just -i at the command line.
Quote:
Finally, if you have just one more moment to spare along the way, what's the rule for quoting "string" and 'string' in a context such as this:
Code:
@ARGV = ("test.txt", 'test2.txt');
Both single and double quotes seem to work interchangeably without error or warning: Is the code agnostic when it comes to such conventions???
You live or die by quotes in perl. There are even quote operators like q//, qq//
Any string surrounded by double quote allows interpolation or interpretation, like resolving the variable and scape symbols: e.g. $line, \n
Single quote do not interpolate variables or interpret especial characters, they are taken at face value.
Greetings!
Here's what I believe is a "simple one" for the community tonight ;)
What I'm trying to do is assign a "true/false" value to a variable depending upon whether a named process (some-process) exists; and then test for this value in the succeeding logic. I banged my head against the... (2 Replies)
Hi,
I have a text file where all records come in one line (single line file), each record starts with 'BUCH' and ends with '@&' and if data is not there we get space instead. between '@&' and next record there might be some spaces, now I want to remove those spaces between '@&' and 'BUCH'.
... (4 Replies)
Hallo everybody,
I have a following problem - I'm doing a map funciont to fill in a HTML table and I want to use some radiobutton groups. Unfortunatelly, they are grouped by names, so I have to add some "counter" that will divide one row from another, and I'm using CGI.pm for generating the... (3 Replies)
Dear Friends,
I want to replace following line with given line.
It should grep/search following string in a file (input.txt)
M/M SRNO: 000M/6200-0362498 COSMETIC PRO MALE FEMALE
Once found it should replace it to following string.
T_DLHNNO: 000M/6200-0362498 ... (7 Replies)
Hey everybody. I've got a simple problem but am unsure how to resolve it. I am using a script to edit multiple files at once. Inside the script I am using an sed command to make the changes. My problem is that I can only get it to work for stings that contain a word or words. How can I modify it to... (1 Reply)
Hi All,
I have below file which has data in below format.
#$ | AB_100
| AB_300
()| AB_4
@*(% | AB-789
i want o/p as below format.
| AB_100
| AB_300
| AB_4
| AB-789
So here there is no standard format.
How we can achieve the same in unix ?
Regards, (3 Replies)
I have one string
string1=user/password:IP_ADDR:Directory
I need to replace string1 value like store into string2
string2=user password:IP_ADDR:Directory
i.e replace "/" character by '<space>' character
But i wouldn't use any file in the meantime.
Please help me......................... (6 Replies)
I want to append a decimal number to a string. But I want to restrict the number to only 2 decimal points
for e.g:
my $output = "\n The number is = ";
my $number = 2.3333333;
$output = $output . $number;
But I want the $output as: "The number is = 2.33"; and not 2.3333333
(I do not... (1 Reply)
I want to replace a string within a file using perl.
We have a line that gets commented out, and I want to replace that line now matter how it was commented out.
for example, I'd want to replace
###ES=PR1A with ES=PR1A
or
##LJW(9/16/26)ES=PR1A with ES=PR1A
I tried: perl... (4 Replies)
Hi guys, I hope you can help me with my problem.
I have a text file that contains lines like this:
78 ANGELO -809.05
79 ANGELO2 -5,000.06
I need to find all occurences of amounts that are negative and replace them with x's
78 ANGELO xxxxxxx
79... (4 Replies)