file conversion in perl

Posted on 2009-02-23
Last Modified: 2012-05-06
I am taking an input file with a list of names. then for every occurence of Clark Kent, I am switching it with SUPERMAN!!!. I have some simple code below. where am i going wrong?
#!/usr/bin/perl -w

open("name.txt", "<");

open("nameout.txt", ">filename.out");



        my($line) = $_;


        if($name = Clark Kent)


                $newnme = "SUPERMAN!!!";

                my($first, $last) = split(//, $name);

                $line = sprintf($name, $newnme );





Open in new window

Question by:swinfosec
    LVL 1

    Expert Comment

    One thing I see is that you cannot compare a string with "=", and the string must be quoted.  it must be like:

    if ($name eq "Clark Kent") {
    LVL 48

    Accepted Solution

    You have a lot of code you don't need.

    You could do it as a one liner, ie:

    perl -pe 's/Clark Kent/Superman!!!/' >filename.out

    or use the code below
    use strict;
    use warnings;
    open NAME, "" or die "Can not open $!\n";
    open NAMEOUT, ">filename.out" or die "Can not open filename.out $!\n";
    while(<NAME>) {
      s/Clark Kent/SUPERMAN!!!/;
      print NAMEOUT;

    Open in new window


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now