Solved

Need to convert sed expression to a similar awk expression

Posted on 2012-04-12
3
571 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
  • 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CentOS 7 Installation 7 73
wipe a usb using python 5 48
awk to variable in bash 2 43
Using Linux to replace Windows Server 2008 R2 for network drives 5 73
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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