Regular expression issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular expression issue
# 1  
Old 12-02-2008
Regular expression issue

Hi,

I have the following string: $string= BG_1_12345_?_XX.SVF

The '?' means that any character could be in that position.

If from a file i will read the string $str=12345 how can i modify the $str in order to be $str=$string.

Best regards,
Christos
# 2  
Old 12-02-2008
Clarify your question, its vague what you're trying to achief.

Regards
# 3  
Old 12-02-2008
Hi,

try:

Code:
if [[ "$string" =~ "$str" ]] ; 
then     
    echo $(sed "s/\(.*\)[0-9]\{5\}\(.*\)/\1${str}\2/" <<< $string ); 
fi

If $string contains the numbers of $str, then
use sed to read in the part in frot of the matched numbers "\1"
and the part after the matched numbers "\2",
put the content of $str between both and echo the result.


Another solution would be:

Code:
echo $(sed "s/\(.*\)${str}\(.*\)/\1${str}\2/" <<< $string )

HTH Chris
# 4  
Old 12-03-2008
What i would like to do is from the $str string to create the $string string by appending/prepending the correct characters. The problem is with the characters ? which is a character that i do not know.
I would like to do this in perl.

Thank you
Christos
# 5  
Old 12-03-2008
Hi,

here the same in perl:

Code:
perl -e '$string="BG_1_12345_X_XX.SVF"; $str=12345; 
        if ( $string =~ /(.*)$str(.*)/ ){$front=$1;$end=$2;printf "$1$str$2"}'

This assigns $string a value, $str contains a substring of $string.
Then we test if $string matches $str and save the part in front and after
$str in two variables $front and $end. Finally the concatenation of
$1$str$2 is print out.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl regular expression not inbetween issue

Dear All, perl -e 'if($ARGV =~ /\A^{10}\z/){print "Valid string\n"}else{print "Invalid string\n"}' '1234567899' Valid string perl -e 'if($ARGV =~ /\A^{8}\z/){print "Valid string\n"}else{print "Invalid string\n"}' '12345678' Valid string individually both works, anyway to combine both... (3 Replies)
Discussion started by: jimmy_y
3 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Shell Programming and Scripting

regular expression and ls

hello using KSH shell i have those files in a folder FILE01 FILE1 FILE02 FILE2 FILE001 FILE0001 in a script i would like to list all the files through regular expressions i tried this ls FILE+* but i receive this error ls: 0653-341 The file FILE+* does not exist. what is... (2 Replies)
Discussion started by: ade05fr
2 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

7. Shell Programming and Scripting

AWK script issue for the part regular expression

Hi I am having a file as shown below FILE 1 TXDD00, TXDD01, TXDD02, TXDD03, TXDD04, TXDD05, TXDD06, TXDD07, TXDD08, TXDD09, TXDD10, TXDD11, TXDD12, TXDD13, TXDD14, TXDD15, TXDD16, TXDD17, TXDD18, TXDD19, TXDDCLK, TXDJTAGAMPL0, TXDJTAGAMPL1,... (3 Replies)
Discussion started by: jaita
3 Replies

8. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

9. Shell Programming and Scripting

regular expression help

hello all.. I'm a bit new to this site.. and I hope to learn alot.. but I've been having a hard time figuring this out. I'm horrible with regular expressions.. so any help would be greatly appreciated. I have a file with a list of names like this: LASTNAME, FIRSTNAME, MIDDLEINITIAL how can... (5 Replies)
Discussion started by: mac2118
5 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question