LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing a Single Line with 2 Matching Queries? (https://www.linuxquestions.org/questions/linux-newbie-8/editing-a-single-line-with-2-matching-queries-4175626410/)

anon3001 03-26-2018 11:01 PM

Editing a Single Line with 2 Matching Queries?
 
Hi,

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

netapp:/mount /mountpoint nfs4 rw 0 0
server1:/mount2 /mountpoint2 nfs rw 0 0

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:

netapp:/mount /mountpoint nfs4 rw,user_xattr,_netdev 0 0

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!

syg00 03-26-2018 11:21 PM

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.

anon3001 03-26-2018 11:51 PM

Sed is what I usually do. I'll look into how it works with addressing. I was think to use awk also.

syg00 03-27-2018 03:15 AM

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.

AwesomeMachine 03-27-2018 01:56 PM

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.

chrism01 03-28-2018 10:55 PM

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.

anon3001 03-30-2018 12:26 AM

Think I got it.

syg00 03-30-2018 01:09 AM

Maybe. - I saw your post prior to the edit to remove it. Very rude when asking people to spend their time helping you.

anon3001 03-30-2018 01:35 AM

it's flawed since it assumes certain things that may or may not be in place, hence the edit. I'm working on finding a better solution.

MadeInGermany 03-30-2018 03:45 AM

With awk/shell
Code:

#!/bin/sh
fstab=/etc/fstab
if awk '
  $1~/netapp:/ {
    $3="nfs4"
    $4="rw,user_xattr,_netdev"
  }
  { print }
' $fstab > $fstab.new
then
  cmp $fstab.new $fstab || {
    cp -p $fstab $fstab.old &&
    cp $fstab.new $fstab &&
    rm $fstab.new
  }
fi


anon3001 03-31-2018 12:02 AM

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:


awk '$1 ~ /^netapp:/ && $4 !~ /,minorversion=/{ $4 = $4 ",minorversion=1" } {print}' /etc/fstab


for nfs to nfs4 conversion:

sed -i '/^netapp:/ s/nfs/nfs4/g' /etc/fstab

AwesomeMachine 03-31-2018 01:33 AM

How about this
Code:

grep netapp /etc/fstab | sed 's;nfs4;nfs;'

MadeInGermany 04-04-2018 11:20 AM

Likewise in awk, instead of
Code:

$4="rw,user_xattr,_netdev"
you can change a part of it with sub()
Code:

sub("minorversion=[^,]*", "minorversion=1", $4)
The RE
Code:

minorversion=[^,]*
covers (and substitutes) any following non-comma characters.


All times are GMT -5. The time now is 10:58 PM.