Unable to get substring from a string in perl


 
Thread Tools Search this Thread
Top Forums Programming Unable to get substring from a string in perl
# 1  
Old 03-06-2012
Unable to get substring from a string in perl

Hello friends,

I have one string "Absoulte_Markup_XYZ.xml" . I am trying to get XYZ in a variable but not able to Smilie
Here please note that "XYZ" can be "XYZABC" or "XYZPQR"...i mean to say its length is variable. I hope you got my point.

I tried with
> `grep <Above String> | cut -f2 -d.`
Result=Absoulte_Markup_XYZ
but not sure how to go further to get XYZ.

Please help me out...
# 2  
Old 03-06-2012
Does the below suffice your requirement ?
Code:
Result=$(echo "Absoulte_Markup_XYZ.xml" | cut -d "." -f1 | awk -F "_" '{print $NF}')

This User Gave Thanks to codemaniac For This Post:
# 3  
Old 03-06-2012
Thanks a lot ... Smilie it worked...Smilie
# 4  
Old 03-06-2012
Quote:
Originally Posted by harpal singh
...
I have one string "Absoulte_Markup_XYZ.xml" . I am trying to get XYZ in a variable but not able to
Here please note that "XYZ" can be "XYZABC" or "XYZPQR"...i mean to say its length is variable. ...
Code:
$
$
$ str="Absoulte_Markup_XYZ.xml"
$
$ echo $str | perl -plne 's/^.*_(.*?)\..*$/$1/'
XYZ
$
$
$ str="Absoulte_Markup_XYZABC.xml"
$
$ echo $str | perl -plne 's/^.*_(.*?)\..*$/$1/'
XYZABC
$
$
$ str="Absoulte_Markup_XYZPQR.xml"
$
$ echo $str | perl -plne 's/^.*_(.*?)\..*$/$1/'
XYZPQR
$
$

tyler_durden

---------- Post updated at 01:09 PM ---------- Previous update was at 01:04 PM ----------

Code:
$
$ echo $str
Absoulte_Markup_XYZPQR.xml
$
$ echo $str | cut -d_ -f3 | cut -d\. -f1
XYZPQR
$
$ echo $str | awk '{split($0,x,"[_.]"); print x[3]}'
XYZPQR
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Date substring from a string

Hi, I have 2 statements in a file a.sh start time is Fri Jan 9 17:17:33 CST 2015 a.sh end time is Fri Jan 9 17:47:33 CST 2015 I am required to get only the time out of it. like 17:17:33 & 17:47:33 PLs suggest (21 Replies)
Discussion started by: usrrenny
21 Replies

3. UNIX for Dummies Questions & Answers

get substring from string variable

Hi Dears, How to use variable in string location of expression ${string:index:length} to get substring from string? I encounter error "bad substitution" when I use expression ${$var:0:5} to get first 5 characters from $var. Could you please help me out of this? Thanks! (2 Replies)
Discussion started by: crest.boy
2 Replies

4. Shell Programming and Scripting

Help with string and substring also I/O

#!/bin/sh PRINTF=/usr/bin/printf PASSWD=/etc/passwd $PRINTF "Enter a UserID\n" read USERID if ; then $PRINTF "$USERID does not exist, please contact IT service\n" exit 1 fi USERHOME=`grep "^$USERID:" $PASSWD | awk -F : '{print $6}'` USERSHELL=`grep "^$USERID:"... (1 Reply)
Discussion started by: ikeQ
1 Replies

5. Shell Programming and Scripting

get substring from string

Hi All, Problem Description: XML_REP_REQUEST=`CONCSUB "$LOGIN" "SQLAP" "$RESP_NAME" "$USRNM" WAIT="Y" "CONCURRENT" "APPLICATION_SHORT_NAME" "CP_SHORT_NAME"` echo Report Request: $XML_REP_REQUEST --to print value in log file While execution the value of 'XML_REP_REQUEST' is 'Prozess... (5 Replies)
Discussion started by: suman.g
5 Replies

6. Shell Programming and Scripting

Want the substring of a string with special characters using perl

Hi, I am trying to separate the directory from the filename using the substring and it does not like it. $Complete_name="C:\ABCD\Reports_Split\090308.1630" I want $File_Name as 090308.1630 I tried using Substring function and it does not like slashes in the complete_name. Any... (5 Replies)
Discussion started by: sushma0907
5 Replies

7. UNIX for Dummies Questions & Answers

How to get the substring from the string

Hi All, Can anybody help me to get the substring from the given string. (3 Replies)
Discussion started by: Anshu
3 Replies

8. Shell Programming and Scripting

Removing substring from a string

Hi. I want to remove a substring from a string. If I use the "tr" command, it doesn't seem to to what I want it to: echo './somestring.dat' | tr -d './' results in somestringdat I want it to remove only occurances of the string './', rather than the individual character. The result... (1 Reply)
Discussion started by: dmilks
1 Replies

9. Shell Programming and Scripting

getting a substring from a string

hi all, I am trying to extract SUBSTRINGS out of a string using ksh. The string is "SAPR3K.FD0.FA.TJ.B0010.T050302" I tried using a= `expr substr $stringZ 1 2` which is giving me a syntax error, donno why?? any ideas why its not working?? I also tried echo "welcome" | awk '{... (3 Replies)
Discussion started by: maradona
3 Replies

10. Programming

can i get a substring from a string?

for example, the string a is "abcdefg", can i get a substring "bcd" (from ato a) from string a? thank you (4 Replies)
Discussion started by: dell9
4 Replies
Login or Register to Ask a Question