Deleting - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Deleting

EsmebabeEsmebabe Posts: 7Member
Is it possible to delete certain sentences or word in a file.Look at the following text below.

->begin
su00106 ka tsatsi la hlano kgwedi
su00106
ka
tsatsi
la
hlano
kgwedi
su00107 supa tharo lefeela
su00107
supa
tharo
lefeela
->End

I want to delete those lines with numerics. I mean,the following lines in the text above:

su00106 ka tsatsi la hlano kgwedi
su00106

su00107 supa tharo lefeela
su00107

So that i can be left with only the following words in a file"

ka
tsatsi
la
hlano
kgwedi
supa
tharo
lefeela


Is it possible?





Comments

  • XfactorXfactor Posts: 343Member
    [b][red]This message was edited by Xfactor at 2004-2-17 2:23:10[/red][/b][hr]
    : Is it possible to delete certain sentences or word in a file.Look at the following text below.
    :
    : ->begin
    : su00106 ka tsatsi la hlano kgwedi
    : su00106
    : ka
    : tsatsi
    : la
    : hlano
    : kgwedi
    : su00107 supa tharo lefeela
    : su00107
    : supa
    : tharo
    : lefeela
    : ->End
    :
    : I want to delete those lines with numerics. I mean,the following lines in the text above:
    :
    : su00106 ka tsatsi la hlano kgwedi
    : su00106
    :
    : su00107 supa tharo lefeela
    : su00107
    :
    : So that i can be left with only the following words in a file"
    :
    : ka
    : tsatsi
    : la
    : hlano
    : kgwedi
    : supa
    : tharo
    : lefeela
    :
    :
    : Is it possible?

    When you're using Perl for text processing/parsing, anything is possible. Something like this should work:

    @new_list = grep !/d+/, @original_list;

    I tested this and it does work.

    X


  • EsmebabeEsmebabe Posts: 7Member
    #!/usr/local/bin/perl -w

    my $words="C:/mana-wordlist.txt";

    my $out="c:/output.txt";
    I'm having the following errors:

    Name "main::prompt" used only once: possible typo at C:ScriptsDelnumerics.pl line 8.
    Name "main::new_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    Name "main::original_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    Use of uninitialized value in string at C:ScriptsDelnumerics.pl line 18.

    With this code:

    # open prompt file
    open my $fh, "< $words" || die ("Unable to open prompt file $prompt");

    open my $wfh, "> $out" || die ("Can't create output file!");

    # Read every word into an array
    my @words = <$fh>;
    close $fh;

    my %temp = ();
    @new_list = grep !/d+/, @original_list;
    print $wfh "$_";

    close $wfh;
  • XfactorXfactor Posts: 343Member
    : #!/usr/local/bin/perl -w
    :
    : my $words="C:/mana-wordlist.txt";
    :
    : my $out="c:/output.txt";
    : I'm having the following errors:
    :
    : Name "main::prompt" used only once: possible typo at C:ScriptsDelnumerics.pl line 8.
    : Name "main::new_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    : Name "main::original_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    : Use of uninitialized value in string at C:ScriptsDelnumerics.pl line 18.
    :
    : With this code:
    :
    : # open prompt file
    : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    :
    : open my $wfh, "> $out" || die ("Can't create output file!");
    :
    : # Read every word into an array
    : my @words = <$fh>;
    : close $fh;
    :
    : my %temp = ();
    : @new_list = grep !/d+/, @original_list;
    : print $wfh "$_";
    :
    : close $wfh;

    I made up variable names in the example I gave you. You have to substitute your variable names that I gave with yours. So guessing by your code,

    Change
    open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    to
    open my $fh, "< $words" || die ("Unable to open prompt file $words");

    @new_list = grep !/d+/, @original_list;
    to
    @new_list = grep !/d+/, @words;

    print $wfh "$_";
    to
    print $wfh "@new_list";

    Also, I don't see any reason why you need this line:
    my %temp = ();

    X
  • JonathanJonathan Posts: 2,914Member
    All that has been said is fine, just an aditional thought...

    : : #!/usr/local/bin/perl -w
    : :
    : : my $words="C:/mana-wordlist.txt";
    : :
    : : my $out="c:/output.txt";
    : : I'm having the following errors:
    : :
    : : Name "main::prompt" used only once: possible typo at C:ScriptsDelnumerics.pl line 8.
    : : Name "main::new_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    : : Name "main::original_list" used only once: possible typo at C:ScriptsDelnumerics.pl line 17.
    : : Use of uninitialized value in string at C:ScriptsDelnumerics.pl line 18.
    : :
    : : With this code:
    : :
    : : # open prompt file
    : : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    : :
    : : open my $wfh, "> $out" || die ("Can't create output file!");
    : :
    : : # Read every word into an array
    : : my @words = <$fh>;
    : : close $fh;
    : :
    : : my %temp = ();
    : : @new_list = grep !/d+/, @original_list;
    : : print $wfh "$_";
    : :
    : : close $wfh;
    :
    : I made up variable names in the example I gave you. You have to substitute your variable names that I gave with yours. So guessing by your code,
    :
    : Change
    : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    : to
    : open my $fh, "< $words" || die ("Unable to open prompt file $words");
    :
    : @new_list = grep !/d+/, @original_list;
    : to
    : @new_list = grep !/d+/, @words;
    :
    : print $wfh "$_";
    : to
    : print $wfh "@new_list";
    No need for the quotes. ;-)

    Note that if it's a very bit file, reading it all into an array sometimes isn't a good idea. You can use a loop to deal with this and put something like:-

    next if /d/;

    In it. For more info on why you sometimes need to do it this way, this thread explains...
    http://www.programmersheaven.com/c/msgboard/read.asp?Board=12&MsgID=241592&Setting=A9999F0001

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.