0

I'm trying to parse text file which has multiple patters. Goal is to have everything in between * * and only integer in between ^ ^ it should remove all special character or string if found.

data.txt

*ABC-13077* ^817266, 55555^
*BCD-13092* ^CL: 816933^
*CDE-13127* ^  ===>   Change 767666 submitted^

output.txt

ABC-13077 817266 55555
BCD-13092 816933
CDE-13127 767666

my script

#!/usr/bin/perl
use strict;
use Cwd;
my $var;

open(FH,"changelists.txt")or die("can't open file:$!");
while($var=<FH>)
{
    my @vareach=split(/[* \s\^]+/,$var);
    for my $each(@vareach)
    {
        print "$each\n";
    }
}
3
  • Are the * and ^ just markers, or do they really exist in the input? Commented Apr 3, 2015 at 15:07
  • they do exist in the input. Commented Apr 3, 2015 at 15:09
  • but you do want to have multiple numbers in a line seperated? Commented Apr 3, 2015 at 15:21

1 Answer 1

3

Replace the while loop with the following:

while (<FH>) {
    s/\*(.*)\*/$1/;
    s/\^(.*)\^/ join ' ', $1 =~ m([0-9]+)g /e;
    print;
}

The first substitution removes the asterisks.

The second substitution takes the ^...^ part, and replaces it with the result of the code in the replacement part because of the /e modifier. The code matches all the integers, and as join forces list context on the match, it returns all the matches.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick help Choroba.. but i'm getting error "Can't find string terminator "'" anywhere before EOF at -e line1." can i run as "perl -pe 's/*(.*)*/$1/;s/\^(.*)\^/ join " ", $1 =~ m([0-9]+)g /e' data.txt"
windows.. perl v5.8.3
@Mihir: Oh, I see. Try switching the quotes.
I did try, now i'm getting "Substitution replacement not terminated at -e line 1."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.