perl: Help with commenting out specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: Help with commenting out specific lines
# 1  
Old 04-17-2012
perl: Help with commenting out specific lines

Hi
I'm trying to comment out specific lines from /etc/fstab file, for simplicity I'm trying to use perl one liner but it errors out,

Below is the Perl oneliner I'm using,
PHP Code:
perl -wlp --'BEGIN{$flag=0}if (!/root/) && (!/boot/) && (!/tmpfs/) ) {$flag =1;} elsif (/^$/) {$flag=0} if ($flag) {s/^/#/;}' /tmp/fstab

# cat /tmp/fstab
/dev/rootvg/root        /                       ext4    defaults        1 1
/dev/rootvg/home        /home                   ext4    defaults        1 2
/dev/rootvg/usr         /usr                    ext4    defaults        1 2
/dev/rootvg/tmp         /tmp                    ext4    defaults        1 2
/dev/rootvg/opt         /opt                    ext4    defaults        1 2
/dev/rootvg/var         /var                    ext4    defaults        1 2
/dev/rootvg/core        /var/core               ext4    defaults        1 2
LABEL
=/boot             /boot                   ext3    defaults        1 2
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
/dev/rootvg/swap        swap                    swap    defaults        0 0
/dev/testvg/logslv /logs ext4 defaults 1 2
/dev/testvg/backuplv /backup ext4 defaults 1 2
/dev/testvg/dbcachelv /dbcache ext4 defaults 1 2 
Error:
PHP Code:
syntax error at ./fstab.pl line 2near "i -e "
Execution of ./fstab.pl aborted due to compilation errors
---------- Post updated at 05:10 PM ---------- Previous update was at 02:29 PM ----------

Just an fyi...I'm able to comment/uncomment if I grep for just one word but not for multiple words, it comments out any line not containing 'root' word in the file /tmp/fstab...for eg..
PHP Code:
perl -wlp --'BEGIN{$flag=0}if (!/root/) {$flag =1;} elsif (/^$/) {$flag=0} if ($flag) {s/^/#/;}' 
# 2  
Old 04-18-2012
Try:
Code:
perl -wlp -i -e 'BEGIN{$flag=0}if (!/root|boot|tmpfs/) {$flag =1;} elsif (/^$/) {$flag=0} if ($flag) {s/^/#/;}' /tmp/fstab

I would use (btw):

Code:
perl -pi -e 's/^/#/ if !/root|boot|tmpfs|^$/' /tmp/fstab


Last edited by Klashxx; 04-18-2012 at 04:25 AM..
# 3  
Old 04-18-2012
Thanks much, below code worked like charm,
PHP Code:
perl -pi -'s/^/#/ if !/root|boot|tmpfs|^$/' /tmp/fstab 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

2. Shell Programming and Scripting

Commenting Multiple lines using Shell Script

I have an xml file which has following code : <abc-ref> <abc-name>abc.efg.hij.klm</abc-name> </abc-ref> I want to comment this whole section out and I have written the following script : (where "hij" is unique string in the file) TEMPFILE=replaceYY.tmp file=hello.xml sed -n... (6 Replies)
Discussion started by: Dish
6 Replies

3. UNIX for Dummies Questions & Answers

Commenting multiple lines

Hi, Can anyone let me know how to comment multiple lines in VI editor? Many thanks. Regards, Venkat. (3 Replies)
Discussion started by: venkatesht
3 Replies

4. Shell Programming and Scripting

Commenting lines in Shell script

Hi All, I know we can comment by using "#" .... I want to know... is there any way to comment a whole big script easily.... In a file i need to comment more than 15 lines ........ and check the script and un comment back. I am learning VI now so its taking lot of time to comment and un... (4 Replies)
Discussion started by: firestar
4 Replies

5. Shell Programming and Scripting

commenting out lines between two delimiters

Hi All, I am struggling to get my head around the following issue. I am having to comment out lines between two delimiters by placing an asterix in position 7 but retain all lines in the file and in the same order. so for example a file containing: ... ... DELIM1 ... ... DELIM2... (2 Replies)
Discussion started by: Bruble
2 Replies

6. Shell Programming and Scripting

Commenting lines in a file using SED

Hi, I need to comment the below lines in a file using sed.These are the few lines of the jsp file that need to be commented. if(top.location != location){ top.location.href = location.href; } Using the below command two lines can be commented: if(top.location != location){ ... (9 Replies)
Discussion started by: meetu
9 Replies

7. Shell Programming and Scripting

Commenting lines

Hi can any body pls help me : I have a file Which Content is like following: p3:s1234:powerfail:/usr/sbin/shutdown -y -i5 -g0 >/dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/critagt > /dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/startcia.sh... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

8. Shell Programming and Scripting

Commenting xml file lines

Hi , I have a XML file like this <dependency> <groupId>fr.xxxx.portail.ear</groupId> <artifactId>_xxxEAR</artifactId> <version>1.0.0-20xxxxx.xxxxx-x</version> <type>ear</type> </dependency> I need to comment single/multiple lines from XML file. How can i... (6 Replies)
Discussion started by: scorpio
6 Replies

9. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

10. UNIX for Dummies Questions & Answers

Commenting lines

How to comment a set of lines in a script? we use # to comment a single line , is there ant other cmd to comment a block? (2 Replies)
Discussion started by: rolex.mp
2 Replies
Login or Register to Ask a Question