LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing a file using sed within a script (https://www.linuxquestions.org/questions/linux-newbie-8/editing-a-file-using-sed-within-a-script-931598/)

t_tunewiki 02-27-2012 01:12 PM

Editing a file using sed within a script
 
Hello all.

I am trying to create a script that uses sed to edit a line in the /etc/fstab on our servers.

Here is the output of the /etc/fstab file:

# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc nodev,noexec,nosuid 0 0
# / was on /dev/sda1 during installation
UUID=07739fd5-63c5-4c7c-9c87-f8102ce98041 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=149c3128-4b11-443e-bfc9-d29d8798daae none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
nas1.shared.day1.tunewiki.net:/var/www/albumart /var/www/albumart nfs _netdev,bg,rsize=8192,wsize=8192,timeo=14,intr
nas1.shared.day1.tunewiki.net:/var/api /var/api nfs rsize=8192,wsize=8192,timeo=14,intr

Basically, I want to replace the mount on line 16 with our new mount. I'm new to sed and I've been trying to make sense of the man page as well as some other online examples, but I can't figure out the exact syntax for what it needs to be.

Any advice would be greatly appreciated. Thanks.

t_tunewiki 02-27-2012 02:06 PM

I tried to figure out how to delete this thread, but couldn't. I actually found a solution.

My solution was just this command:

Code:

sudo sed "s*nas1.shared.day1.tunewiki.net:/var/www/albumart on /var/www/albumart type nfs (rw,bg,rsize=8192,wsize=8192,timeo=14,intr,addr=172.16.10.59)*192.16.10.2:/var/www/albumart*g" -i sed_test.txt

David the H. 02-28-2012 09:57 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

You can't delete threads and you shouldn't worry about trying. Just post the solution you found, as you did, and leave it for others to learn from.


Some comments on your solution though. First of all, the string you have above doesn't match the fstab syntax at all. That's the format for the mount command's default output.

It appears you want only to replace the share's source field, and keep everything else the same, correct? So what you really need on the left side is a regex that will match only the first field, and its replacement on the right side.

Code:

sed -i 's|^nas1.shared.day1.tunewiki.net:/var/www/albumart|192.16.10.2:/var/www/albumart|' /etc/fstab
I used "|" as the substitution delimiter above, but any ascii character works, as long as it's not found in the expression itself. Note also how you don't need the "g" option at the end, since you're only doing a single substitution.


By the way, if you want to replace the entire line, it's generally better to use an address field to target it, and the "c" (change) command to do the replacement, instead of "s" (substitute).

Code:

sed -i '/^nas1.shared.day1.tunewiki.net/ c 192.16.10.2:/var/www/albumart /var/www/albumart nfs _netdev,bg,rsize=8192,wsize=8192,timeo=14,intr' /etc/fstab
Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

I highly suggest taking the time to work through the basic parts of the first tutorial at least.

t_tunewiki 02-28-2012 12:28 PM

David the H,

Thanks for the advice. Your examples should be plenty for me to work with. It was also helpful to know that any delimiter could be used in place of a backslash.

Thanks again.


All times are GMT -5. The time now is 06:34 PM.