Solved

Need to convert sed expression to a similar awk expression

Posted on 2012-04-12
3
576 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 500 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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

717 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