GREP Searching for a newbie...

Hi,

I really need some help with GREP searching...

I need to find all occurances of a file reference and remove two characters from the end of the reference. For example, here are a few lines showing the text:

<image file="STRAIGHT_004CR.jpg" ALT="STRAIGHT_004CR.jpg" />
<image file="STRAIGHT_006CR.jpg" ALT="STRAIGHT_006CR.jpg" />
<image file="STRAIGHT_007CR.jpg" ALT="STRAIGHT_007CR.jpg" />

And they then need to look like:

<image file="STRAIGHT_004.jpg" ALT="STRAIGHT_004.jpg" />
<image file="STRAIGHT_006.jpg" ALT="STRAIGHT_006.jpg" />
<image file="STRAIGHT_007.jpg" ALT="STRAIGHT_007.jpg" />

I think I've written the correct search query which I think is:

_\d{3}\D\D

But I have no idea how to write the replacement string to remove the additional letters at the end of the string of 3 numbers and leave everything else in tact...

ANy help would be much appreciated...

Thanks

Steve.

Hi,

try using :

sed. sed 's/CR.jpg/.jpg/g'

But make sure you have a backup of the file.
regards

Thanks for this...

Not sure i was very clear before, the text I need to replace has the last two letters vary so I can't specificaly request a change on a specific set of characters like 'CR'.

The text string I need to replace is made up of varying 3 digits and then 2 letters like:

000XX

But I need to remove the two letters in each instance but preserve the rest of the text around the string as in the example:

<image file="STRAIGHT_000XX.jpg" ALT="STRAIGHT_000XX.jpg" />

needs to look like:

<image file="STRAIGHT_000.jpg" ALT="STRAIGHT_000.jpg" />

I am using a text editing program to do this which supports GREP find and replace so need to know the GREP search query string which I think is _\d{3}\D\D and then the Replacement string which will remove the two letters at the end of the text which it finds...

Sorry to be a pain...

Thanks.

sed 's/\(.*\)\(..\)\(....\)/\1\3/' filename
sed 's/[A-Z][A-Z].jpg/.jpg/g' <file-name>

Cheers,
K

Thanks guys...!

Should work a treat...!

All the best!!

hi matrixmadhan or any gurus,
Can you kindly explain how "sed 's/\(.*\)\(..\)\(....\)/\1\3/' filename" which u have mentioned above works.

thanks in advance....

() braces are used to tage the matched string, . is used to refer to a single character, numbers(1 and 3 in the code) is used to refer to the tags that is first ( \(.*\) ) and third( \(....\)/ ).

the logic would go like this: it will tag a word as the first tag, then following 2 characters as the second tag and the following 3 characters as the third tag. it will search for the whole string specified within /Search string/ and replace it with the first and third tags.

the above code is not working for me in ksh. so, here is an alternative.

echo "<image file="STRAIGHT_000XX.jpg" ALT="STRAIGHT_000XX.jpg" />" |sed 's/\(..\).jpg/.jpg/g'

output:

hi matrixmadhan...

shouldnt that search string be

"/\(.*\)\(..\)\.\(...\)/\1\.\3/" ???