Register
It is currently Sat Nov 22, 2014 11:57 am

need sed to ignore spaces (and tabs) between fields


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Nov 15, 2013 2:30 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
I am trying to modify /etc/fstab to set every ext4 partition that has 1 2 to use nodev,nosuid. Here is what I have and it works:

a portion of /etc/fstab:
Code:
<...blah> /                      ext4    defaults        1 1
<...blah> /boot                  ext4    defaults        1 2
<...blah> /var                   ext4    defaults        1 2


Code:
sed -i 'ext4    defaults        1 2/ {/nodev,/nosuid/! s|defaults|defaults,nodev,nosuid|}' /etc/fstab


that would make this:
Code:
<...blah> /                      ext4    defaults        1 1
<...blah> /boot                  ext4    defaults,nodev,nosuid      1 2
<...blah> /var                   ext4    defaults,nodev,nosuid      1 2


But what if the spaces/ tabs between the parition and "ext4" and then between "ext4" and "defaults" were not the same everytime. How can I rewrite this to ignore any white space, but achieve the same result?

Thanks.


Top
 Profile  
 PostPosted: Sat Nov 16, 2013 11:13 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
It's a bit tricky to do.

Code:
sed -i.bak '/ext4\([[:space:]]\)\1*defaults\1\1*1\1\1*2/{/nodev,nosuid/!s/defaults/&,nodev,nosuid/}' /etc/fstab

...don't like the fact that the string « nodev,nosuid » appears twice on the RHS.


With an actual editor, ex(1):
Code:
ex -sc $'g/\\vext4\s+defaults(,nodev,nosuid)@!\s+1\s+2/s/defaults/&,nodev,nosuid/\nx' /etc/fstab

...don't like the fact that you obviously can't capture the group that's not matching.


And for the sake of doing it properly:
Code:
ex -sc $'let s=",nodev,nosuid"|g/\\vext4\s+defaults(\=s)@!\s+1\s+2/s/defaults/\=submatch(0).s/\nx' /etc/fstab

Don't forget to back up your file.


Top
 Profile  
 PostPosted: Mon Nov 18, 2013 8:38 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Thanks! That last one did it perfectly! Now time for me to dechiper how it worked :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP