grep ^M in file using perl script....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep ^M in file using perl script....
# 1  
Old 02-05-2008
grep ^M in file using perl script....

hi
i am using perl on windows ( active state perl 5.8 ) and i want to check for Control-M (^M) in file. files to be checked are in unix format so new line character is (\n). This perl script is called from Batch file ( windows .BAT file )
my script is
Code:
          while (<PROGRAM>) {
                  chomp $_ ;
                  if ( /^M/ ) {  # ^M - control-v + control-M
	         $useError = $1 ;
                      $checkFail++ ;
                      printf STDOUT "LineNo %6d : [$_] - \'Cntl+M\' used.\n", $. ;
                  }
             }
             close PROGRAM ;

this script is not working because when i do chomp perl removes ^M ( as its a new line character in windows ) so how can i check for ^M on windows perl ? ( it works if ^M is in between line )

reason i am doing this on windows is that this BATCH file is executed when somebody check in file into PVCS ( which is on windows server ) as all files are for unix servers they should not have ^M and i cant use SFU perl if i do that then i have to make sure that everyone using PVCS should have SFU installed on their system. so you must be wondering who puts ^M in this case some times people opens the file in notepad and then saves the file.
# 2  
Old 02-05-2008
you can remove them in unix using dos2unix
# 3  
Old 02-05-2008
Code:
sed -e 's/^M//g' file > newfile

^M must me untered with CTRL+V, CTRL+M
# 4  
Old 02-05-2008
Quote:
Originally Posted by ghostdog74
you can remove them in unix using dos2unix
i dont want to remove ^M what i want is when user runs this script with 1 input parameter as file to be checked for ^M it should tell if file hash ^M or not ..

like
Code:
$ ./check.pl test.lst
LineNo     42 : [       xxxxxxxxxxxxxxxxx xxxxxxx ^M] - 'Cntl+M' used.
1 Lines needs to be changed.

and i can not use Unix commands here not even sed,awk i only have perl ( windows version ) installed. this script is wrapped in windows BATCH file.
# 5  
Old 02-05-2008
Code:
#!/usr/bin/perl
while (<>) {
   print if $_ =~ /\cM\n/g;;
}

# if you want to remove it
#while (<>) {
 #  print if $_ =~ s/\cM\n/\n/g;;
#}

# 6  
Old 02-05-2008
Question

Quote:
Originally Posted by ghostdog74
Code:
#!/usr/bin/perl
while (<>) {
   print if $_ =~ /\cM\n/g;;
}

# if you want to remove it
#while (<>) {
 #  print if $_ =~ s/\cM\n/\n/g;;
#}

hey thanx for the reply this code works perfectly on UNIX but when i execute it on windows it does not work. in both the cases same file was used.

even the code i have written works ...but not on windows ..
# 7  
Old 02-05-2008
Quote:
Originally Posted by zedex
hey thanx for the reply this code works perfectly on UNIX but when i execute it on windows it does not work. in both the cases same file was used.

even the code i have written works ...but not on windows ..
in windows, it should not be ^M. you can try \r\n . otherwise, i am out of ideas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep the exact process in perl script

I am working on one script where I need to grep the exact process in perl script. for e.g. when I run simple grep command on the linux machine it gives me two process mGateway_mvc_q01 and mGateway_mvc_q01_v7 which is not the correct result.I tried to use ( ps -eAf | grep ^mGateway_mvc_q01$) but... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

2. Shell Programming and Scripting

Need a perl script similiar to grep -r 'word' /path/to/dir"

Hi , i am looking for a perl script to grep for a string in all files inside a directory . bash command . grep -r 'word' /path/to/dir Thanks, Nvil (3 Replies)
Discussion started by: nevil
3 Replies

3. Shell Programming and Scripting

shell or perl script using grep and regex

Hi, I have file stored in a directory containing information about subnet mask and next hop address in the following format 10.1.1.0/16, 255.255.0.0, 10.1.1.1 10.1.2.0/16, 255.255.0.0,10.1.2.1 here 10.1.1.0/16 represent range of ip address 10.1.1.1-10.1.1.16 given say an IP address... (1 Reply)
Discussion started by: termeric
1 Replies

4. Programming

Unix grep in perl script

Hello, Fairly simple really I have an xml file and I want to check to see if it contains a pattern. The pattern is "../" On the command line I can type: grep "\.\./" myFile.xml and I get desired result. To do the same thing in a perl script I thought it was as simple as putting the ``... (4 Replies)
Discussion started by: Jaymoney
4 Replies

5. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

6. Shell Programming and Scripting

Perl - Grep open file more then once.

Hi, I am using File::Find to go through a very large tree. I am looking for all xml files and open only those that contain a tag <Updated>. I then want to capture the contents of two tags <Old> and <New>. My problem is, after I open the file and do the first grep for <Updated> (which does... (3 Replies)
Discussion started by: OldGaf
3 Replies

7. Shell Programming and Scripting

pattern grep using Perl in .TSV file

Hi All, I have a .TSV extension file having ~1 Gig data and I need to grep a pattern in that file using perl. I am not able to read the file using perl any suggestions on this/ If I Change the format my data gets mismangled so I am bothered about using specific format as well. #!... (3 Replies)
Discussion started by: vmsenthil
3 Replies

8. Shell Programming and Scripting

Need to grep 2 words from Perl Script results in Terminal....

Hey guys. I have a .pl script that scans my hosts to see if they are down or up. I can run it anytime I want. The script uses a conf file that contains text lines of the IP addresses of the servers. I run the script from the command line of my terminal (MAC OS) I run: sudo ./scanner.pl brings... (3 Replies)
Discussion started by: yoyoyo777
3 Replies

9. Shell Programming and Scripting

[Help] PERL Script - grep multiple lines

Hi Gurus, I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element. 1) Generation ID 2) Month 3) Year... (6 Replies)
Discussion started by: miskin
6 Replies

10. Shell Programming and Scripting

doing grep inside perl file

Hi have one Perl file inside that i am defining at an array file. @temp_vmdk_files = `grep vmdk '$guest_vmx'` where my $guest_vmx=/vmfs/volumes/47e40fec-9c8bb7f7-d076-001422159f8a/BES Exchange/BES-Exchange.vmx and i am just want to do grep of "vmdk" files from the above path but when... (5 Replies)
Discussion started by: bp_vardhaman
5 Replies
Login or Register to Ask a Question