Modifying sed to only change last occurrence.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modifying sed to only change last occurrence.
# 1  
Old 09-25-2012
Modifying sed to only change last occurrence.

I'm using sed to switch integers (one or more digits) to the other side of the ':' colon. For example: "47593:23421" would then be "23421:47593". The way it functions right now, it is messing my settings file to use with gnuplot. The current command is:

Code:
sed 's/\([0-9]*\):\([0-9]*\)/\2:\1/' out3 > plotted.p

How can I modify this to only change the very last occurrence in the text file?

Last edited by Scrutinizer; 09-26-2012 at 01:58 AM.. Reason: code tags
# 2  
Old 09-25-2012
What I often do is make the GNUplot script itself a BASH script, which I execute and feed the output into gnuplot's stdin, or a temporary file. Things which need to vary can be fed in via commandline parameters. A bit more flexible, and a lot more direct, than dozens of little fiddly sed things.

This'd be tricky even in awk, because of the way it needs to put back changes it made earlier... It would have no way of knowing when the end of file is coming, after all. The script that generates it in the first place might.
# 3  
Old 09-26-2012
This is pretty simple using ed (assuming that there is only one pair of colon separated numbers on the last line in out3 that contains a colon and that there are no colons that don't have digits before and after the colon):
Code:
#!/bin/ksh
ed -s out3 <<-EOF
        H
        1s/^//
        ?\([0-9]*\):\([0-9]*\)?s//\2:\1/
        w plotted.p
EOF

I use ksh, but any shell that uses here-documents as specified by the POSIX Standards (including at least bash, ksh, and sh) will work.
# 4  
Old 09-27-2012
Thanks everybody for the help! I checked out 'ed' and learned some new tools to use. I ended up attacking the problem from a different point of view (more creative but it works the way I need it to). Smilie
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. UNIX for Dummies Questions & Answers

Substitution in first occurrence with sed

I have the following script: sed '/string1/,/string2/!d' infile I want to apply the script to the first occurrence only. I have tried sed '0,/string1/,/string2/!d' infile Of course, that does not work Any help will be greatly appreciated (12 Replies)
Discussion started by: Xterra
12 Replies

3. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

4. Shell Programming and Scripting

Sed diffrent replace by occurrence

I couldn't find the answer anywhere, so I hope you could help me. I need to change something like the following: something/bla/aaaa anything/bbb to: something --bla ----aaaa anything --bbb How do I do this? Is it possible with sed? I tried various patterns, but don't know how to... (5 Replies)
Discussion started by: Patwan
5 Replies

5. UNIX for Dummies Questions & Answers

Modifying a file using SED

Dear Members, I have a file which contains data as shown below: LOAD DATA APPEND INTO TABLE xxap.test ( RAW_DATA char(3000) "replace(:raw_data,'*','|')", LOADING_SEQUENCE "xx_gl_bank_fee_s1.nextval", CREATION_DATE SYSDATE, LAST_UPDATE_DATE SYSDATE, ATTRIBUTE1 "DATE_STAMP", ... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

6. Shell Programming and Scripting

force to change password(by modifying /etc/shadow)

hi by modifying /etc/shadow how can I Force a change of password so that user has at least 1 week to login? I did it by using: echo "enter username to force password change" read user; chage -M 7 $user; How can I do it by modifying /etc/shadow?? (6 Replies)
Discussion started by: tjay83
6 Replies

7. Shell Programming and Scripting

change home directory by modifying passwd

hi How can I change the home directory of a user without using usermod -d command? ( by modifying /etc/passwd) (17 Replies)
Discussion started by: tjay83
17 Replies

8. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question