?
Solved

Need to convert sed expression to a similar awk expression

Posted on 2012-04-12
3
Medium Priority
?
577 Views
Last Modified: 2012-04-19
I am trying to re-engineer the following function in a script so that it can use awk to search for the /home mount in fstab and comment out the line. The reason being the method for mounting the /home directory can vary... eg, UUID, LVM path, Label... etc

So basically I just need the first line below changed so that it uses awk to search for /home instead of using sed to un-comment the /dev/mapper/os_vg-home line

function setupautofs {
if [ $DEV ]; then
	umount /home
	sed -i.orig -e 's/^\/dev\/mapper\/os_vg-home/#\/dev\/mapper\/os_vg-home/' /etc/fstab
	sed -i.orig -e '/+auto.master/i\/home     /etc/auto.home' /etc/auto.master
	echo "*     -_netdev,nosuid,tcp,hard,bg,intr,rsize=32768,wsize=32768     alanetapp001b.choicepoint.net:/vol/home/&" >> /etc/auto.home
	chkconfig autofs on
	service autofs restart
fi
}

Open in new window


Here is the fstab if needed:

UUID=1d086e6d-83f0-4a9c-984f-186dc80eda3f /boot                   ext4    defaults        1 2
/dev/mapper/os_vg-home  /home                   ext4    defaults        1 2
/dev/mapper/os_vg-tmp   /tmp                    ext4    defaults        1 2
/dev/mapper/os_vg-var   /var                    ext4    defaults        1 2
/dev/mapper/os_vg-swap  swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

Open in new window

0
Comment
Question by:Thaidog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 37837156
awk cannot modify files "in place" as sed can do with the "-i" flag and thus can't save a backup copy either.

So you will have to redirect awk's output to a temporary file to then move this file back to the original.

The awk statement could look like this:

awk '{if($0~/^\/dev\/mapper\/os_vg-home/) print "#" $0; else print $0}' /etc/fstab > /tmp/fstab.$$ && mv /tmp/fstab.$$ /etc/fstab

Replace "mv" with "cp" to keep a backup copy of fstab in /tmp, suffixed with the current PID of your script.

wmp
0
 
LVL 1

Author Comment

by:Thaidog
ID: 37850768
This seems to do what I need:

awk '$2 == "/home"{$0 = "#"$0}{print > FILENAME}' /etc/fstab
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37850845
Are you sure?

I think you'll end up in an infinite loop appending lines to fstab until the FS is full.

This is an alternative (but possibly dangerous - make a copy of /etc/fstab first!):

( rm /etc/fstab && awk '$2 == "/home"{$0 = "#"$0} {print}' >/etc/fstab ) </etc/fstab

or shorter

( rm /etc/fstab && awk '$2 == "/home"{$0 = "#"$0}1' >/etc/fstab ) </etc/fstab



wmp
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Fine Tune your automatic Updates for Ubuntu / Debian
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question