[SOLVED] Editing a Single Line with 2 Matching Queries?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
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.
Im sure this has been asked before, but wasn't sure of the proper query for it. I have several servers that may or may not be using nfs in /etc/fstab. What I want to do is write a conditional statment that first checks (simple enough). The part that I'm having trouble with is editing "nfs" fstype only on the netapp entries. The end result should look something like this
So I would need only the netapp entries to be edited to "nfs4", but leaving any other nfs mounts alone as "nfs". Also, there may be several netapp mounts in any given server if that matters.
Also, is it possible to append new options if the existing options are random from server to server? The above is simplified, but many mounts may have a variation like this:
There is no consistency with the option contents or their order, but am curious how I can possibly insert "minorversion=1" in this field considering its randomness. Thanks!
First up, you really, really, really don't want to be screwing with fstab unless you are 100% sure you got it right. Bad things happen on the next reboot, although you can check before then.
Are we talking sed here ?. If so, the first bit should be trivial - it uses addressing to limit lines selected; and word bounding will ensure you don't get false hits on "nfs".
As for the insertion, pretty simple regex to add it to the end of the options if say you always have fields 5 and 6 present.
awk will certainly also do it, it has all the regex support. It may also be easier to work with fields for adding that extra info. As always, it's your choice.
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
I would place in the script to keep a backup fstab, and run diff between the two > stdout. As syg00 has stated, machine edits on fstab can be troublesome. My recommendation would be grep on netapp, piped to sed.
I'd use Perl for this, definitely creating a backup as the first step (fstab.YYYYMMDD).
Read the whole file into a hash using the first field as the key; it has to be unique anyway.
Then use Perl's string/regex handling to sort out the nfs thing, then create a new fstab.new file and eyeball the first few goes until you are confident.
You can get the prog to copy the .new file over the live one, maintaining the backup version anyway - it's a small file.
You could even create a fstab_dir and keep multiple versions in there ... auditing & reversal uses.
Thank you very much for this. My only concern with it would be the $4 part. I'm implementing this on several hundred servers, and there's not usually consistency in that field. Some may have rw only, others may have a variation of what's in your script, but in a different order, etc... Would it still work those cases?
Here's what I have so far. It should address minorversion part:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.