Replacing the last occurence of a expression in every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the last occurence of a expression in every line
# 1  
Old 02-15-2010
Replacing the last occurence of a expression in every line

I have a file which contains

Code:
 
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[2\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/XRdDataGaterEnx


I have to replace the last occurence of /X in each line
ie I need my output

Code:
 
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/RdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/RdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[2\]/RdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/RdDataGaterEnx

Please suggest me a way to do the same..

Thnx
Naveen
# 2  
Old 02-15-2010
Code:
awk -F "" '{$(NF-14)=""}1 ' OFS="" input.txt

# 3  
Old 02-15-2010
How did you get the 14? Is it generic ? If I have varied string lengths like

Code:
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/XRdDataGaterEnx
/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[2\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/XRdDataGaterEnx

will this work??

THanks

---------- Post updated at 04:28 PM ---------- Previous update was at 04:23 PM ----------

And the code doesnt work for the last line as there are 3 additional characters

---------- Post updated at 04:30 PM ---------- Previous update was at 04:28 PM ----------

ie if i have

Code:
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/XRdDataGaterEnx
/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[2\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/XRdDataGaterEnxEnx

then 14 will not work right? So is there any other way out?
# 4  
Old 02-15-2010
Code:
 sed 's/X//3' input.txt

# 5  
Old 02-15-2010
I am very very sorry should have given a proper code...

Can you suggest one last time

what if I have

Code:
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/XRdDataGaterEnx
/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/I3/XIrdOutEnRdSbComb\[2\]/XRdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/XRdDataGaterEnxEnx

I need the output


Code:
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[0\]/RdDataGaterEnx
/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[1\]/RdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/I3/XIrdOutEnRdSbComb\[2\]/RdDataGaterEnx
/RdWrCtrl/IRdWrRepCtl/XI3/XIrdOutEnRdSbComb\[3\]/RdDataGaterEnxEnx

basically all i need to do is replace the last occurence of /X with /
# 6  
Old 02-15-2010
Code:
sed -e 's/\(.*\)\/X\(.*\)$/\1\/\2/g' abc.txt


HTH,
PL
# 7  
Old 02-15-2010
yes that helps could you please explain the code?? Thnks so much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vi delete line with second occurence of pattern

I have a large file and many lines are duplicated. I'm trying to delete lines with every second occurrence of a pattern. Did tried searching similar question but no luck. I can delete all lines matching pattern with :g/pattern/d but don't want to lose data. Sample pattern to delete... (6 Replies)
Discussion started by: homer4all
6 Replies

2. Shell Programming and Scripting

How to get line after occurence of sequence of patterns

In the past I needed a help with the problem how to search for pattern after the occurence of another pattern which is described in this thread: https://www.unix.com/shell-programmin...-pattern1.html Now I would need something quite similar, only the pattern which is to be searched must be... (3 Replies)
Discussion started by: sameucho
3 Replies

3. Shell Programming and Scripting

Replacing nth occurence

Hi My input file is like this for eg: abc abc abc abc abc abc i would like to replace "abc" with "cba" where the occurrence is divisible by 2 of eg here 2nd, 4th and 6th occurence shud be replace can anyone suggest in awk or sed (11 Replies)
Discussion started by: raghav288
11 Replies

4. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

5. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

6. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

7. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies

8. Shell Programming and Scripting

Sed and replacing one occurence of pattern

I would like to use sed to replace one occurence of a pattern in a file. When I use the s/// command it replaces all occurences of the pattern in the file. Should I be using something other than sed? Thanks (6 Replies)
Discussion started by: ss9u
6 Replies

9. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

10. Shell Programming and Scripting

first occurence and line number

Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... .. total rejected rows: 40 total rejected rows: 50 total rejected rows: 80 total rejected rows: 90 total... (9 Replies)
Discussion started by: mercuryshipzz
9 Replies
Login or Register to Ask a Question