LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-26-2018, 11:01 PM   #1
anon3001
LQ Newbie
 
Registered: Jan 2016
Posts: 28

Rep: Reputation: Disabled
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!

Last edited by anon3001; 03-26-2018 at 11:08 PM.
 
Old 03-26-2018, 11:21 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 03-26-2018, 11:51 PM   #3
anon3001
LQ Newbie
 
Registered: Jan 2016
Posts: 28

Original Poster
Rep: Reputation: Disabled
Sed is what I usually do. I'll look into how it works with addressing. I was think to use awk also.
 
Old 03-27-2018, 03:15 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 03-27-2018, 01:56 PM   #5
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 03-28-2018, 10:55 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 03-30-2018, 12:26 AM   #7
anon3001
LQ Newbie
 
Registered: Jan 2016
Posts: 28

Original Poster
Rep: Reputation: Disabled
Think I got it.

Last edited by anon3001; 03-30-2018 at 12:48 AM.
 
Old 03-30-2018, 01:09 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Maybe. - I saw your post prior to the edit to remove it. Very rude when asking people to spend their time helping you.
 
Old 03-30-2018, 01:35 AM   #9
anon3001
LQ Newbie
 
Registered: Jan 2016
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-30-2018, 03:45 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
Old 03-31-2018, 12:02 AM   #11
anon3001
LQ Newbie
 
Registered: Jan 2016
Posts: 28

Original Poster
Rep: Reputation: Disabled
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

Last edited by anon3001; 03-31-2018 at 12:05 AM.
 
Old 03-31-2018, 01:33 AM   #12
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
How about this
Code:
grep netapp /etc/fstab | sed 's;nfs4;nfs;'
 
Old 04-04-2018, 11:20 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed command for matching the end character of a line and replacing the whole line. mamunm Linux - Newbie 4 04-12-2014 12:19 AM
Queries on RHCS command line tools chirag_linuxforum Red Hat 2 01-22-2010 12:20 AM
Perl only matching single-character regex patterns? Lordandmaker Programming 3 01-20-2009 08:59 AM
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration