I have input text file e.g myfile.txt which is contains data like


etc. only phone number is changing I have some 13 work orders like this I need to take only number as a input from this for that I need a script in perl. I need to create that script I am trying grep but I am unable get only numbers.


Looking at your input file you can do something like this, using awk:

awk -F"_" '{print $3}' inputfile | uniq > outputfile

or using grep,

grep -o -E '[0-9]+' inputfile | uniq > outputfile

using sed,

sed 's/[^0-9]*//g;/^\s*$/d' inputfile | uniq 

no need for uniq

awk -F"_" 'NF>2 {if ( !a[$3]++) print $3}' inputfile


  • NF>2 ensure a phone number is present
  • !a[$3]++ will evaluate to 1 first time, 0 other time

You can utlizise this perl script for your perpose:

    my $str = "moveover_virus_7483920322_virus.csvwork";
    my  $phone = (split /_/, $str)[2];
    print "$phone\n";

test the script:

    [iahmad@ijaz-cms ~]$ ./perltest 
  • but I need to take input from a text file which is having 100 work orders like this and I need to delete the repeated values. – rajaputh Jul 21 '16 at 6:44
  • yes u can do that , this is just an example , you can read the file in perl and use a loop through the lines and final output the file – Ijaz Ahmad Khan Jul 21 '16 at 8:18

I am a fan of look arounds to do this kind of work. I copied you example data into a text file named "test" and then ran the following

$ grep -oP '(?<=s\_).*(?=\_v)' test 

grep -o means return only the match

-P means use Perl regex

?<= means "match anything that comes after s_" (note that the "_" needs to be escaped with the "\")

?= means "match anything that comes before v_" (again note that the "_" needs to be escaped)

So the end result is a match for anything between "s_" (virus_) and "-v" (_virus)

  • could you please give me the complete script I am getting errors .I am unable to debug why because I don't know anything about perl – rajaputh Jul 21 '16 at 9:48
  • @rajaputh This is complete. It is a one-line command using grep with the Perl regular expressions. Note that the .* means to match anything between the two regular expressions in the sets of parentheses. – rcjohnson Jul 25 '16 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.