LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-27-2012, 01:12 PM   #1
t_tunewiki
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 02-27-2012, 02:06 PM   #2
t_tunewiki
LQ Newbie
 
Registered: Feb 2012
Posts: 3

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

Last edited by t_tunewiki; 02-28-2012 at 12:23 PM.
 
Old 02-28-2012, 09:57 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

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

Last edited by David the H.; 02-28-2012 at 09:59 AM.
 
1 members found this post helpful.
Old 02-28-2012, 12:28 PM   #4
t_tunewiki
LQ Newbie
 
Registered: Feb 2012
Posts: 3

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
editing file with script B-Boy Programming 4 09-02-2009 08:23 AM
Editing a File with SED SBN Linux - General 16 05-18-2008 08:05 AM
Need help no editing a file through a script procfs Programming 7 07-19-2006 05:59 AM
multiple file editing using... sed? perl? bpk Linux - Software 2 10-07-2005 04:16 AM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

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

All times are GMT -5. The time now is 03:52 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration