[SOLVED] Need to edit last word on each line in file
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Rep:
Need to edit last word on each line in file
Hi All
I have a file which a number at the end of each line. I need to change this number in the file to be correct, i.e. each time the number is 9 it needs to be 1, each time it is 233 it needs to be 2, etc... There is no pattern to the numbers currently in the list other than the same number appears only in a single line/group of lines, not throughout the file, but the replacements need to be sequential (but can be repeated an arbitrary number of times).
I've looked at both sed and awk, but I'm coming really unstuck with the regular expressions, I've used both in the past for simple things, but in this case what I want to change is a variable, so I don't think I can use a regexp and it is at the end of the line which doesn't seem easy with sed.
If you can offer any advice I would be really grateful!
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Original Poster
Rep:
Hi rikijpn
Sorry for the delay in replying, it was a bank holiday weekend here! I did not know if I could use a variable in the sed expression, all the examples I have seen seemed to avoid the topic.
I'm going to have a good look through the grymoire so that I fully understand your suggestion.
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Original Poster
Rep:
I really appreciate that - I'm working on the sed suggestion at the moment, and I've been trying a few examples which only change the last occurence of a pattern in echoed line but have yet try variables in it!
I am actually a little more familiar with awk myself (though rarely for more than printing columns of data) and understand your suggestion better.
I'll probably implement something like the awk suggestion but will pursue the sed one as well to fill in some gaps in my knowledge, it's proving more versatile than I realised!
You can do all sorts of arcane things with sed - doesn't make it the best tool for the job. I skipped awk - went from sed to perl.
I'm trying to get up to speed on awk, so I can understand you wanting to investigate sed for your own edification.
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Original Poster
Rep:
Hi All
syg00's solution seems to be the quickest to implement and works amazingly "out of the box", I've declared the two variables needed before the if and all the tests I've been running so far have been great.
I'm going to incorporate some other bits as I need my output formatted specifically, but this solution was really useful.
Thanks everyone for your tips - you've helped me to understand my own ignorance bit better ;-)
I have a file which a number at the end of each line. I need to change this number in the file to be correct, i.e. each time the number is 9 it needs to be 1, each time it is 233 it needs to be 2, etc... There is no pattern to the numbers currently in the list other than the same number appears only in a single line/group of lines, not throughout the file, but the replacements need to be sequential (but can be repeated an arbitrary number of times).
I've looked at both sed and awk, but I'm coming really unstuck with the regular expressions, I've used both in the past for simple things, but in this case what I want to change is a variable, so I don't think I can use a regexp and it is at the end of the line which doesn't seem easy with sed.
If you can offer any advice I would be really grateful!
Cheers
Emma
Hi There, I might be a little bit confused by what you said, but what I normally do to change from one carackter to another one, making a global change within a file, vi the file ad type the following command:
:1,$s/9/1/g ( nd press enter )
This command will change all 9's to 1's in the file ...
Usefull ?
Last edited by linuxgurusa; 06-01-2010 at 08:46 AM.
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Original Poster
Rep:
Unfortunately it isn't as simple as replacing all "9" with "1" because the entire file is 7,000,000 lines of 14 columns of numbers!
Anyway, rather inelegantly shown, but it is only a short term solution to re-ingest data which was lost from a couple of databases due to some buggy scrpts I was given. This is what I ended up with, first I removed all the lines starting with the string "PulseId" (i.e. the headers from my original MySQL queries), then if the line did not start with that string, I altered the numbers and printed the output in the required format!
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL
Posts: 55
Original Poster
Rep:
Ah - before you all point out the silly bug, I've seen it! I didn't test it on enough lines!
[Edit]
Problem solved - setting up the initial values of the variables was a bad idea as the command works on every line in the file! To solve the problem I've simply removed last_var=233 and new_val=1. I've tested this several times and it seems to work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.