LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2012, 06:41 PM   #1
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Rep: Reputation: 32
Question I need an awk statement that will look for the home directory and comment it out.


I need an awk statement that will look for the home directory and comment it out in fstab. The reason is we have multiple ways of defining the device path for differt servers so sed will not work all the time. Does anybody know a simple way to do this with awk?
 
Old 04-14-2012, 07:04 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What are those multiple ways? It's hard to say anything without knowing what you mean.
 
Old 04-14-2012, 07:06 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Sorry if I'm missing something but why wouldn't it work?


Code:
sed '/home/ s/^/#/' /etc/fstab
 
Old 04-14-2012, 07:28 PM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
if "sed" does not work reliable how can "awk" do?
Here is a bash only solution:
Code:
while read dev mountpoint dummy;
do
    if [[ $mountpoint = "/home" ]] ; then
        printf "#$dev $mountpoint $dummy\n" > fstab.new
        continue ;
    fi  
    printf "$dev $mountpoint $dummy\n" > fstab.new  ;
done < fstab
mv fstab.new fstab
 
Old 04-14-2012, 07:42 PM   #5
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by sycamorex View Post
What are those multiple ways? It's hard to say anything without knowing what you mean.
The problem is that it could be mounted by path, uuid or label so a simple sed option will not work for all instances. If we can use awk to search the second feild it would work to keep the script working with mount styles.
 
Old 04-14-2012, 07:44 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by Thaidog View Post
The problem is that it could be mounted by path, uuid or label so a simple sed option will not work for all instances. If we can use awk to search the second feild it would work to keep the script working with mount styles.
Ok, but how can you identify the second column of the right row if the naming conventions vary?

Last edited by sycamorex; 04-15-2012 at 06:19 AM.
 
Old 04-14-2012, 10:04 PM   #7
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by sycamorex View Post
Ok, but how can you identify the second column of the right row is the naming conventions vary?
No the second column is the mount point /tmp, /home, ... etc - so that always stays the same... the first column is what is goofy.
 
Old 04-15-2012, 06:16 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hence if you have a common item then either sed or awk will both work fine:
Code:
sed -i '/\/home/s/^/#/' fstab

awk '$2 == "/home"{$0 = "#"$0}{print > FILENAME}' fstab
As such an important file I am guessing you will test with example prior to implementation
 
Old 04-15-2012, 06:21 AM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by Thaidog View Post
No the second column is the mount point /tmp, /home, ... etc - so that always stays the same... the first column is what is goofy.
Yes, that's right. So either I'm really missing something or sed or awk are just the right tools for that. See grail's or my sed examples above.
 
Old 04-16-2012, 12:21 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You know, instead of just giving vague descriptions and making people guess at your exact requirements, you could post an actual example of what you have, along with any conditions or variations that could be encountered, and save everyone some time.

How To Ask Questions The Smart Way

And as demonstrated you shouldn't assume the use of any specific solution (at least not without a clear reason to do so). Focus on what you want to accomplish, not how you think it should be done.
 
Old 04-16-2012, 01:02 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@David - couldn't agree with you more, in fact I think it should be a provision of joining the site that you need to have read the whole thing and agreed to having done so prior to
account activation.
 
Old 04-16-2012, 06:16 AM   #12
Thaidog
Member
 
Registered: Sep 2002
Location: Hilton Head, SC
Distribution: Gentoo
Posts: 637

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by grail View Post
Hence if you have a common item then either sed or awk will both work fine:
Code:
sed -i '/\/home/s/^/#/' fstab

awk '$2 == "/home"{$0 = "#"$0}{print > FILENAME}' fstab
As such an important file I am guessing you will test with example prior to implementation
The awk statement works well thanks for your help.
 
Old 04-17-2012, 06:48 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Incidentally, I want to apologize for coming across as a bit harsh in my last post. I was a bit tired, stressed out, and rushed for time yesterday.

I hope you'll take my comment as a bit of friendly advice, rather than a rebuke. Remember, it helps you to get better answers too!
 
  


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
[SOLVED] Problem with if statement in awk in AIX sukhdip AIX 7 07-29-2015 01:18 AM
[SOLVED] Getting -nan in output of awk statement sparker1970 Linux - Newbie 3 04-06-2012 10:45 AM
[SOLVED] bash script is reprinting part of a field from an awk statement ?? SharpyWarpy Linux - General 13 04-22-2010 03:58 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:12 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
Open Source Consulting | Domain Registration