capture till first occurrence.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting capture till first occurrence.
# 1  
Old 06-03-2009
Bug capture till first occurrence.

Hi,

In my file I have following to lines
-------------------------------------------
C:/India/Apple/Mumbai/Red_Apple/Apple.txt

cd C:/India/Apple && run
-------------------------------------------


I want to replace "C:/India/Apple" with "D:/Test/Red_Apple" whereever the pattern "C:/India/Apple" occurs in my file.

what I want is to capture the first occurrence of "Apple" after "C:" in the line.

Please help me out.

I am trying
s/C:\/.*?Apple/D:\/Test\/Red_Apple/g

but it is replacing "C:/India/Apple/Mumbai/Red_Apple/Apple.txt" to "D:/Test/Red_Apple.txt"

Note: C:/India/Apple can be anything. Meaning it can be C:/India/Mango/Apple etc etc.

Last edited by benitdhotekar; 06-03-2009 at 03:34 AM.. Reason: Updated
# 2  
Old 06-03-2009
Should be something like:

Code:
sed 's!C:/India/Apple!D:/Test/Red_Apple!g' file

# 3  
Old 06-03-2009
Thanks Franklin52 but I already knew this trick.

Please read query carefully. What I want is to capture first occurrence of "Apple" after "C:" in the line. between C:\ and \Apple there can be anything.
Hence the solution provided by you is applicable only in the case where there is C:\India\Apple.
But in my various files it would not always be C:\India\Apple, it can be anything like
C:/India/Mango/Apple or C:/Mumbai/Jaipur/Mango/Apple

I want a regex for this.
# 4  
Old 06-03-2009
Quote:
Originally Posted by benitdhotekar
Please read query carefully.
I did:

Quote:
Originally Posted by benitdhotekar
I want to replace "C:/India/Apple" with "D:/Test/Red_Apple" whereever the pattern "C:/India/Apple" occurs in my file.
# 5  
Old 06-03-2009
Pls post the test cases.

What would be the input and output your expecting clearly.

since you mentioned :

benitdhotekar
Code:
I am trying
s/C:\/.*?Apple/D:\/Test\/Red_Apple/g 

but it is replacing "C:/India/Apple/Mumbai/Red_Apple/Apple.txt" to "D:/Test/Red_Apple.txt"

wht is the Output your expecting in this case ?.
# 6  
Old 06-03-2009
Hi Panyam,

The output expected is as follows:

D:/Test/Red_Apple/Mumbai/Red_Apple/Apple.txt

That means I want to replace the text starting from "C:\" till first occurrence of "\Apple\" by "D:/<some_dir>/Apple".
Between "C:\" and first occurrence of "\Apple" there can reside any text.

The output I am getting by using the above mentioned regex is that it takes the last occurrence of "Apple" in the line whereas I want the first occurrence of "Apple".

Thanks in Advance.

-----Post Update-----

Quote:
Originally Posted by Franklin52
I did:
Hi Franklin,
sorry for that but I meant to say to read the note carefully.
I am looking for a generic regex which would not search for a specific string but will capture till the first occurrence of the expected string i.e. "Apple" in this scenario.
# 7  
Old 06-03-2009
Code:
sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'

TEST:

Code:
TESTING>echo "C:/India/Apple/dd/d/d/d" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/dd/d/d/d
TESTING>echo "C:/India/Apple/d/d/d/Apple.txt" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple.txt
TESTING>echo "C:/India/d/d/d/Apple/d/d/d/Apple.txt" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple.txt
TESTING>echo "C:/India/d/d/d/Apple/d/d/d/Apple" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

3. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

4. Shell Programming and Scripting

Merge lines till a particular word

Hi Experts, I have a requirement like, I have to search between 2 words (<deviceDetails> and </deviceDetails>) and merge all lines in between into 1 line. Example: <deviceDetails><subscriberName>#UNKNOWN#</subscriberName> <customerNumber>#UNKNOWN#</customerNumber>... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

5. Shell Programming and Scripting

Grep with loop till search is done

I need help to put a script where it runs the svn command grep'ing for the ticket# in the comments to see if the ticket was used in the latest commit. so on command line: ./test.sh ticket-1 ticket-2 ticket-3 It should be able to check if ticket-1 is used first and if not then check if... (2 Replies)
Discussion started by: iaav
2 Replies

6. Shell Programming and Scripting

Get the number of lines till I get line

Hi All, I have a file as below: abc.txt ****************************** * HEADER DESCRIPTION ****************************** *Supplier: Prism Customer: MNI -NIGERIA Quantity: 2 Type: PLUG-IN Profile: 70.00 *Subscription: Generic... (5 Replies)
Discussion started by: arunshankar.c
5 Replies

7. Shell Programming and Scripting

Print file till specified line

Hi , I want to print file till line number 33 and stdout to different file. I have used cat -n <file name> which numbers the lines including blank line. now how can i stdout the first 33 lines in to new file. Regards newaix (6 Replies)
Discussion started by: newaix
6 Replies

8. Shell Programming and Scripting

extract till occurance of a string

hi , i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file. below is the extract from the file <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"... (5 Replies)
Discussion started by: sais
5 Replies

9. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

10. UNIX for Dummies Questions & Answers

Delete line till certain point

Hi, I have a requirement to delete a line till a certain word. Am not sure how to do it e.g I want to delete till the bold character since start of line. Any help is higly appretiated. (2 Replies)
Discussion started by: inq
2 Replies
Login or Register to Ask a Question