String Extraction in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String Extraction in Perl
# 1  
Old 12-02-2009
Java String Extraction in Perl

I have a string stored in a variable. For instance,

$str = " Opcode called is : CM_OP_xxx "
where xxx changes dynamically and can be either LOGIN or SEARCH..... depends on runtime.
For example :
$str = " Opcode called is : CM_OP_SEARCH "
$str = " Opcode called is : CM_OP_LOGIN "

I want to display all CM_OP_xxx only, from $str on the console using "PERL".

Sample output:
-----------------
CM_OP_SEARCH
CM_OP_ACT_LOGIN
# 2  
Old 12-02-2009
Code:
$
$ echo " Opcode called is : CM_OP_SEARCH " | perl -lne '/: (.*) / && print $1'
CM_OP_SEARCH
$
$ echo " Opcode called is : CM_OP_LOGIN " | perl -lne '/: (.*) / && print $1'
CM_OP_LOGIN
$
$

tyler_durden
# 3  
Old 12-03-2009
Thanks..
if the string contains " Opcode called is : CM_OP_SEARCH input fucntion"
but if i execute the above statement,
echo " Opcode called is : CM_OP_SEARCH input fucntion" | perl -lne '/: (.*) / && print $1'
the output is coming as "CM_OP_SEARCH input" instead of CM_OP_SEARCH.
Please let me know how to execute this.

2nd Doubt is i want a perl script... how do i embed this in a perl script.
# 4  
Old 12-03-2009
change the perl one liner to
Code:
perl -lne '/: (.*?)\s+.* / && print $1'

The above command is command line perl .
if you want to execute it through a script instead of command line use the following

Code:
my $str = " Opcode called is : CM_OP_xxx ";
print "$1\n" if ($str =~ m/.*: (.*?)\s+.*/);

HTH,
PL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Extraction

I am trying to extract a time from the below string in perl but not able to get the time properly I just want to extract the time from the above line I am using the below syntax x=~ /(.*) (\d+)\:(\d+)\:(\d+),(.*)\.com/ $time = $2 . ':' . $3 . ':' . $4; print $time Can... (1 Reply)
Discussion started by: karan8810
1 Replies

2. Solaris

string extraction won't work. Why?

#!/usr/bin/ksh set -x testfile=my.test.file.flag echo ${testfile: (-4)} #/home/maldohe/scripts/spawn1& sleep 3 echo myspawn is now ending exit Background: I am trying to extract the word flag from anf given file name. This is a demo script that I am working on to fix a production issue.... (8 Replies)
Discussion started by: Harleyrci
8 Replies

3. Shell Programming and Scripting

Sub-string extraction on arrays

Hi, I'm trying to extract the middle of an array that is of variable length but always has a first and last common element, The following works OK... #!/bin/bash ARRAY='switch' ARRAY='option1' ARRAY='option2' ARRAY='option3' ARRAY='value' SWITCH=${ARRAY:0:1} VALUE=${ARRAY:(-1)}... (1 Reply)
Discussion started by: ASGR
1 Replies

4. Shell Programming and Scripting

Function extraction in PERL

the log contains mathematical operation as follows fm_void_mathematics : PCM_OP_MATHS input function PIN_FLD_NUM1 INT 1 PIN_FLD_NUM2 INT 2 PIN_FLD_RESULTS int PIN_FLD_OUT INT * D Wed Sep 16 05:40:22 2009 solaris_testing fm_void_add : PIN_FLD_SUM int 3 D Wed Sep 16 05:40:22 2009... (1 Reply)
Discussion started by: vkca
1 Replies

5. Shell Programming and Scripting

Perl function extraction

The log file reads as follows. D function_add() ADD input data 1021214 0 VAR1 STR 10 0 VAR2 STR 20 0 VAR3 STR 1 SUM=VAR1+VAR2 D function_add() ADD output data 1021267 0 DISPLAY SUM D function_sub() SUB input data 1021214 0 VAR1 STR 10 0 VAR2 STR 20 0 VAR3 STR 1 sub=VAR1-VAR2 D... (2 Replies)
Discussion started by: vkca
2 Replies

6. Shell Programming and Scripting

Need help in string extraction using regular expressions

Hi, I am a new bee to this forum. I am trying to extract the text after a matching pattern from a url using regular expression. Ex: http://locatlhost:2020/proxy/checkthisout I want to extract the string after proxy/. I am not familiar with reg ex. Can someone please help? (2 Replies)
Discussion started by: akatraga
2 Replies

7. UNIX for Dummies Questions & Answers

String extraction from a text file

The following script code works great for extracting 'postmaster' from a line of text stored in a variable named string: string="PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN" stuff=$( echo $string | cut -d@ -f1 | awk '{ print $NF }' ) echo $stuff However, I need to be... (9 Replies)
Discussion started by: cleanden
9 Replies

8. Shell Programming and Scripting

Extraction of the output from a string.

Hi Everyone, I stored the result of a certain awk script in the variable arr.The result is /inets /banking /tools. arr= /inets /banking /tools These are 3 direcctories. I should be able to move in to these directories using "cd" command.Can you tell me how to extract... (5 Replies)
Discussion started by: saicharantej
5 Replies

9. Shell Programming and Scripting

metapattern extraction in PERL

Hi, I want to extract some part of a pattern that matches my requirement in a string with PERL. A case in point is a string like: $eqtst="abh nmae res = 10 s abh nmae req = 10 s"; from which I want the words preceding the "=" symbol. Previously I was assured that there would be only 2 such... (4 Replies)
Discussion started by: Abhishek Ghose
4 Replies

10. Shell Programming and Scripting

String extraction from user input - sh

Hi, I have a shell script to build components of a product. The follow snippet will explain what I am doing. # !/bin/sh for choice in "$@" ; do case $choice in "o") echo "Calling $choice" ; o ;; "i") echo... (8 Replies)
Discussion started by: vino
8 Replies
Login or Register to Ask a Question