Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Comment lines in FSTAB using perl

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 07-13-2012   -   Original Discussion by ajaincv
ajaincv ajaincv is offline
Registered User
Join Date: May 2011
Last Activity: 2 January 2013, 7:04 AM EST
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Comment lines in FSTAB using perl

Hi All,

I need to comment specific Two Lines in fstab & want to do using & also want to ensure that is done corretly.

I am trying the below method. But its giving Search pattern not terminated.


b36376 67 % cat linux-fstab_testing | perl -i -wnl -e '/^('\Q zin46fil01:\/vol\/fil01vol09\/soft1\/\_TOOLS_\/dist \/_TOOLS_\/.dist15 ')/  and print "#$1" or print'
Search pattern not terminated at -e line 1.
b36376 68 %


Please help on this



Last edited by Franklin52; 07-14-2012 at 06:35 AM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
Old Unix and Linux 07-14-2012   -   Original Discussion by ajaincv
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
Join Date: May 2005
Last Activity: 15 November 2017, 4:57 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,633
Thanks: 107
Thanked 1,603 Times in 1,180 Posts
Are you insisting in PERL? Because in fact this is a simple operation and to use PERL for this is probably overkill of the extreme sort. The following solution is sed and will do the job:

sed 's/^[<spc><tab>]*zin46fil01:\/vol\/fil01vol09\/soft1\/\_TOOLS_\/dist \/_TOOLS_\/.dist15/# &/' /etc/fstab > /tmp/sed.$$
mv /etc/fstab /etc/fstab.${DATE}
mv /tmp/sed.$$ /etc/fstab

I assumed the variable "$DATE" here, which is part of my environment, because i like to have date-coded backup files if i let something change via a script. If you don't have such a variable either create one before or even dump the command altogether if you don't need a backup copy.

Replace "<spc>" and "<tab>" with literal spaces/tabs. If you have more than one line to comment out just duplicate the line and change the path accordingly. The "&" in the replacement part means "everything matched by the regexp before":

sed 's/^[<spc><tab>]*\/path\/to\/mount1/# &/
     s/^[<spc><tab>]*\/path\/to\/mount2/# &/' /etc/fstab > /tmp/sed.$$
mv /etc/fstab /etc/fstab.${DATE}
mv /tmp/sed.$$ /etc/fstab

I hope this helps.

Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can i comment out a section between two particular lines proactiveaditya Shell Programming and Scripting 3 02-15-2012 03:29 AM
Trying to show lines in INI files until the comment character (#) hindesite UNIX for Dummies Questions & Answers 4 09-21-2010 07:47 PM
using awk to comment out lines to the end of file phamp008 Shell Programming and Scripting 5 07-05-2009 11:02 PM
comment lines vero_81 UNIX for Dummies Questions & Answers 5 06-11-2009 05:35 AM
how to comment multiple lines in unix balireddy_77 Shell Programming and Scripting 3 02-19-2007 07:16 AM

All times are GMT -4. The time now is 11:50 PM.