GREP Searching for a newbie...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting GREP Searching for a newbie...
# 1  
Old 08-26-2007
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.
# 2  
Old 08-26-2007
Quote:
Originally Posted by steveglevin
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
# 3  
Old 08-26-2007
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.
# 4  
Old 08-26-2007
Code:
sed 's/\(.*\)\(..\)\(....\)/\1\3/' filename

# 5  
Old 08-26-2007
Code:
sed 's/[A-Z][A-Z].jpg/.jpg/g' <file-name>

Cheers,
K
kamitsin
# 6  
Old 08-26-2007
Thanks guys...!

Should work a treat...!

All the best!!
# 7  
Old 08-27-2007
hi matrixmadhan or any gurus,
Can you kindly explain how "sed 's/\(.*\)\(..\)\(....\)/\1\3/' filename" which u have mentioned above works.

thanks in advance....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie looking for how to Grep times more than 10 seconds apart

I am new to grep and Linux and am looking to see if grep can parse out a list of lines that have a difference of more than 10 seconds between the times on each line. Example 2016-09-17 19:30:57 INFO: id: 4562079216, time: 2016-09-17 19:30:41, 2016-09-17 12:02:26 INFO: id:... (26 Replies)
Discussion started by: Markham
26 Replies

2. Shell Programming and Scripting

Insert Text after searching via grep

Hi Team, I have a file with the following patterns: ==> xyz_Server_Started_with_Errors <== errors. ==> abc_Server_Started_with_Errors <== errors ==> reds_Server_Started_with_Errors <== errorss I want them in this format: (5 Replies)
Discussion started by: ankur328
5 Replies

3. Shell Programming and Scripting

Searching for exact match using grep

I am searching for an exact match on a value read from another file to lookup an email address in another file. The file being checked is called "contacts" and it has Act #, email address, and contact person. 1693;abc1693@yahoo.comt;Tommy D 6423;abc6423@yahoo.comt;Jim Doran... (2 Replies)
Discussion started by: ziggy6
2 Replies

4. Shell Programming and Scripting

Searching for multiple patters using grep

i have a file as below grepfile.txt ---------------- RNTO command successful No such file or directory Authentication failed if i seach individually for 'RNTO command successful' or 'No such file or directory' using grep -i as below, im gettting result. grep -i 'No such file or... (5 Replies)
Discussion started by: JSKOBS
5 Replies

5. Shell Programming and Scripting

dynamic string searching for grep

hi my code is something like count=0 echo "oracle TABLESPACE NAME nd TARGET" while do count=`expr $count + 1` (1) tts_space_name$count=`echo $tts | cut -d "," -f$count` (2) target$count=grep $(tts_space_name$count)... (2 Replies)
Discussion started by: Gl@)!aTor
2 Replies

6. Shell Programming and Scripting

grep searching interval

Hi all, I just want to find all values that are in a specified interval. I tryed it with grep e- file , it does not work. Is it possible to get values wich are lower a special number, like grep >e-18 file? Thanks a lot (4 Replies)
Discussion started by: newcommer
4 Replies

7. Shell Programming and Scripting

grep searching

I am making a script but having little problem. at one part I need to find one number format or other format from a file.. those formats are xxx-xx-xxxx or xxxxxxxxx i tried grep '( \{3\}-\{2\}-\{3\} |\{9\})' if i do them sepratly it work but like this it is not working Please check... (7 Replies)
Discussion started by: Learnerabc
7 Replies

8. UNIX for Dummies Questions & Answers

Newbie Help with Grep or Awk .. Easy one ...

I have this output: uniquemember=uid=315kthatch,ou=people,ou=client315,dc=paisleyhosting,dc=com and i want the output to be just this: 315kthatch I need it to be generic tho, because I have hundreds of lines of output, and the preceding numbers are not always 315. So I would need... (3 Replies)
Discussion started by: kthatch
3 Replies

9. Shell Programming and Scripting

searching using grep command

Hi, i have a file called alert_pindb.log i need to grep and count for all the lines starting with "ORA-" but i need to exclude the line which is having "ORA-00600 " i am using following syntax to count the ORA- nos "grep \"ORA-\" alert_pindb.log | wc -l"; since ORA- may be anything... (9 Replies)
Discussion started by: prakash.gr
9 Replies

10. UNIX for Dummies Questions & Answers

Simple newbie grep question

How come grep testfile1 won't find anything in testfile1 (even though the characters sd are there in great quantity), but grep '' testfile1 will find plenty? Do the single quotes prevent the shell from interpreting the testfile1 is interpreted as: grep *test whether or not characters sd exist*... (5 Replies)
Discussion started by: doubleminus
5 Replies
Login or Register to Ask a Question