Removing the last occurance of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing the last occurance of string
# 1  
Old 12-26-2004
Question Removing the last occurance of string

I have a small query. I have a file containing the following lines

abcd<12></12>fdfgdf<12>sdfgfg<12>
sdfsdf<12></12>ytunfg<12>
hggfhf<12>rtysb<12>zdfgdfg<12>

Now I wish to delete ONLY the last occurance of string <12> from every lines of code. That mease my final output will be like this:

abcd<12></12>fdfgdf<12>sdfgfg<12>
sdfsdf<12></12>ytunfg
hggfhf<12>rtysb<12>zdfgdfg
# 2  
Old 12-26-2004
The following sed command will remove the final occurrence of <12> at the end of each line - providing that <12> are the very last characters (i.e. no whitespace)

Code:
sed 's/\(^.*\)<12>$/\1/' oldfile > newfile

If there is whitespace after the <12> you could do something like

Code:
sed 's/\(^.*\)<12>[	]*$/\1/' oldfile > newfile

(Where a space followed by a tab are between the [ square brackets ] ).

Cheers
ZB
# 3  
Old 12-27-2004
Removing the last occurance of a string

Sorry buddy but this command does not works for me.
# 4  
Old 12-27-2004
Well it works for me using the sample data you provided, under three different flavours of Unix.

Why doesn't it work?

Are there any error messages?

Does the output not look the way you want?

Cheers
ZB
# 5  
Old 12-27-2004
Removing last occurence of string

This command command neither gave any erros nor it give me any output, actually the file remains unchanged with this command. In fact the following command works for me

sed 's/\<12\>$//g' <filename>
# 6  
Old 12-27-2004
Quote:
abcd<12></12>fdfgdf<12>sdfgfg<12>
sdfsdf<12></12>ytunfg
hggfhf<12>rtysb<12>zdfgdfg


dkhanna01 !

U mentioned yr required o/p as wrong in the initiial posting.
Please check ....

ZB postings work perfectly to replace the last instance of <12> in the file.
# 7  
Old 12-27-2004
Re: Removing last occurence of string

Quote:
Originally posted by dkhanna01
This command command neither gave any erros nor it give me any output, actually the file remains unchanged with this command.
On some old versions of sed (I'm testing this with UNICOS 9.0 here...), you'll have to modify the sed thusly....

Code:
sed 's/^\(.*\)\<12\>$/\1/' oldfile > newfile

On newer sed (tests here: HP-UX 10.20, Linux circa 2.4, cygwin) the original examples work fine.

Maybe next time post the output of uname -a with your O/P?

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string until 3Rd occurance of forward slash(/)

I have a file abc.txt which has records like 456 /home/fgg/abdc.txt 3567 /home/fdss/vfgb.txt 23 /home/asd/dfght.txt I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH Output should be like abdc.txt vfgb.txt dfght.txt (5 Replies)
Discussion started by: himanshupant
5 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

4. Shell Programming and Scripting

Removing last occurance of string in text

I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote ('). Input text in file is with temp as ( ( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL ( select... (9 Replies)
Discussion started by: Vaddadi
9 Replies

5. 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

6. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

7. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

8. Shell Programming and Scripting

Extracting a substring starting from last occurance of a string/character

Hi All, This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character. for eg. I have a string of a file name with absolute path like... (2 Replies)
Discussion started by: krramkumar
2 Replies

9. UNIX for Dummies Questions & Answers

String search - Command to find second occurance

Hi, I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there... (5 Replies)
Discussion started by: saurabhsinha23
5 Replies

10. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies
Login or Register to Ask a Question