Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to rewrite this Java code in Perl?

Hi,
I have this code in Java. I would like to rewrite it in Perl. How can I do it?

This code basically, opens a file, and if the line starts with s, S or D then we capture the entire line. and keep it in one single variable. So each match will be appended to the previous one with new line character.

		   # public static java.lang.StringBuilder createSubmittedFiles(java.lang.String pathToJobfile){
		   # java.lang.StringBuilder submittedFiles = new StringBuilder();
		   
				# //find the submitted files in the job file 	
		   # Scanner inJob = null;
				# try{
					# inJob = new Scanner(new File(pathToJobfile));
					# java.lang.String jobFile = null;
					# while(inJob.hasNextLine()){
						# jobFile = inJob.nextLine();
						# if(jobFile.startsWith("s ") || jobFile.startsWith("S ") || jobFile.startsWith("D ")){
						# // This is to place an extra space between the file and the revision number
						# jobFile = jobFile.replaceAll("(\\.[A-Za-z]{1,3}) (\\d)", "$1  $2");
						# submittedFiles.append(jobFile).append("\n");
						# }
					# }
				# } 
				# catch(Exception e){
					# logger.error(e);
				# }
				# finally{
					# inJob.close();
				# }
		   # return (submittedFiles);
	   # } //createSubmittedFiles

Open in new window


Avatar of sentner
sentner
Flag of United States of America image

Wouldn't it be much simpler to simply grep for the lines starting with S, s, or D?

egrep '[sSD]' FILE1 FILE2 > RESULTFILE

Avatar of Tolgar
Tolgar

ASKER

right. But there should a space after s, S or D.

How can it be done?

Thanks,
If you want a space after it, you can just include it in the egrep pattern within the quotes, like this:

egrep '[sSD] ' FILE1 FILE2 > RESULTFILE

ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sentner's egrep will work as well but the question asked for perl code (so that's what I provided).
One typo in my egrep BTW.. I neglected the carat at the beginning to signify the line has to start with the s, S, or D...

egrep '^[sSD] ' FILE1 FILE2 > RESULTFILE