ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I write a script that replace some lines following a pattern, using 'sed'.
The script works fine, but now i need to update it to replace only the last occurrence of that pattern en the file.
For example:
#a user
user= foo
name= John
password= 2312
#another user
user= tiger
name= Will
password= 5887
Im searching by the pattern '^user= '.
There is any way to replace ONLY the last occurrence of the pattern?
Hm, I'm sure that in any language You would have to loop through the occurences of ^user and save it in a list/array. Sed has that capability too. The last one received is the one You want to replace. But I couldn't help it, here's one in bash/sed that does the same thing. This is not a help with sed. But I think it illustrates what You want to do and the benefits of the *nix toolbox... I assume the file has a list of records where user is unique, and also that You separate the name with space before it, like:
lakris@ubuntu:~$ cat users.txt
#a user
user= foo
name= John
password= 2312
#another user
user= tiger
name= Will
password= 5887
lakris@ubuntu:~$ new=lion;org=`cat users.txt |grep ^user|tail -1|cut -d" " -f2`;cat users.txt|sed -e "s/^user=\ $org/user=\ $new/"
#a user
user= foo
name= John
password= 2312
#another user
user= lion
name= Will
password= 5887
lakris@ubuntu:~$
of course You could redirect it and do some file renaming and stuff to update the actual "database". lion could be Your input to a script, even the file name. Excercise left to reader...
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!
Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!
Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?
Again, THANKS TO ALL!!!!
PD: Sorry my gad english
Well, *gad* isn't half bad
I have found this, http://www.tldp.org/LDP/abs/html/ , to be a very good resource on bash (and the site http://www.tldp.org/ for most GNU/Linux tools). And of course, man tail, etc is a good way to start. Your distro may have additional documentation packages with HOWTO's and coding examples. A very good way to start is to look at how others solved a specific or similar problem.
I often find myself whipping up one-liners where others would turn to perl or even a higher level programming language. bash and friends are really powerful, fast and intuitive.
Happy hackin'!
Thanks to AL!!!
specially to lakris, your answer is clear and works ok!
Im a begginer in the bash world and there are several commands that i dont know (i.e: tail, cut, head) where can i found a list (or a reference) of that commands and what each do?
Again, THANKS TO ALL!!!!
PD: Sorry my gad english
somewhere down the road, as you progress in shell scripting, you will learn not to use cat for useless purposes such as :
Code:
cat file | grep "blah"
You will also learn how to get what you want without having to use too many pipes. have fun
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.