add an option to the perl script command line [closed] - perl

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
assume this is file.txt
Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final
Annette Adams :8:24:19:24:10:12:43:35:98
Mary Beard:9:30:19:23:10:14:29:39:87
Antoinette Brown:9:16:18:22:9:12:19:31:79
.
.
.
is it possible in Perl to write a script with some options which user can pass to command line and get the result.
for example getAverage.pl -l 3 file.txt prints the average of Lab3 Marks.
if it is possible what is the overall process?

Try http://perldoc.perl.org/Getopt/Long.html From the documentation:
The Getopt::Long module implements an extended getopt function called
GetOptions(). This function adheres to the POSIX syntax for command
line options, with GNU extensions. In general, this means that options
have long names instead of single letters, and are introduced with a
double dash "--". Support for bundling of command line options, as was
the case with the more traditional single-letter approach, is provided
but not enabled by default.
For general information about accessing command line options in Perl, read the perlvar documentation: http://perldoc.perl.org/perlvar.html#$ARGV

I put together a quick and dirty solution in python. Should be easy to port it to perl.
import sys
lines = open("file.txt",'r').readlines()
if len(sys.argv) > 2 and sys.argv[1] == "-l":
try:
number = int(sys.argv[2])
except:
print "Input a number after the -l switch."
average = 0
for i in lines:
try:
average += int(i.split(":")[number])
except:
pass
average /= 3
print "The average score for %s is %d" \
%(lines[0].split(":")[number], average)
else:
print "Computer says no."

Related

Perl script to remove new line character and move next line data to previous line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I have input like below
"ID"|"Desc"
"100"|"
The data present in Desc column has new line characters.
So the data came to second line.
Some records of data went to third line. But I need all data to be present in first line."
"101"|"This record desc is correct data which has present in single line. So I need data to present in single line."
I need output like below,
"ID"|"Desc"
"100"|"The data present in Desc column has new line characters.So the data came to second line.Some records of data went to third line. But I need all data to be present in first line."
"101"|"This record desc is correct data which has present in single line. So I need data to present in single line."
Can someone please help the Perl script where we can achieve above requirement.
Use Text::CSV_XS to process the file as it can parse it correctly.
perl -MText::CSV_XS=csv -wE 'csv( in => shift,
always_quote => 1,
sep_char => "|",
eol => "\n",
on_in => sub { $_[1][1] =~ s/\n//g } );
' -- file.csv > newfile.csv
I'm testing this in a Linux shell, you might need a different eol if you're in MSWin. Also, I don't know what rules Powershell uses for quoting, co you might need to use a different type of quotes.

How to grep the data only for the first two combination and perform > operation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a file as shown below.
2.6G kishan /home/Srikishan
10G kishan /home/data/aa
150G kishan /home/Junk
300G kishan /home/junk2
I want a command which displays only the folders which are consuming more than 50G memory. Can someone help me how I can code it using shell or Perl or TCL.
As a Perl one-liner
perl -ne'/([\d.]+)G/ and $1 > 50 and print' myfile
output
150G kishan /home/Junk
300G kishan /home/junk2
This will also ignore lines that don't contain a field like 999G
And here's the Tcl contender. It looks at every line in the file whose name is in the filename variable and prints those lines that begin with a floating-point number larger than 50.
package require fileutil
fileutil::foreachLine line $filename {if {[scan $line %f] > 50} {puts $line}}
Using awk you can do:
awk -F 'G' '$1>50' file
150G kishan /home/Junk
300G kishan /home/junk2

Should I use sed, awk, perl, for altering text spanning multiple lines and selecting only the info needed? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm working on a project for class where we take a file full of lines describing classes like the one below
CSC 1010 - COMPUTERS & APPLICATIONS
Computers and Applications. Prerequisite: high school Algebra II. History of computers, >hardware components, operating systems, applications software, data communication.
3.000 Credit hours
and turn it into
CSC1010,COMPUTERS & APPLICATIONS,3
I used:
sed -n 's/^CSC /CSC/p' courses.txt > practice.txt
which outputs:
CSC1010 - COMPUTERS & APPLICATIONS
CSC1310 - INTRO COMP PROGRAMMING NON-MAJ
CSC2010 - INTRO TO COMPUTER SCIENCE
CSC2310 - PRIN OF COMPUTER PROGRAMMING
CSC2320 - FUND OF WEBSITE DEVELOPMENT
CSC2510 - THEOR FOUNDATIONS OF COMP SCI
CSC3010 - HISTORY OF COMPUTING
CSC3210 - COMPUTER ORG & PROGRAMMING
CSC3320 - SYSTEM-LEVEL PROGRAMMING
CSC3330 - C++ PROGRAMMING
CSC3410 - DATA STRUCTURES-CTW
CSC4110 - EMBEDDED SYSTEMS
CSC4120 - INTRODUCTION TO ROBOTICS
and I also used:
sed '/\.000 Course hours//p' courses.txt > courses10.txt
which outputs:
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
My problem is trying to select whether the sed, awk, or perl would be better. So, far I've used sed to eliminate the lines that are not composed either of the course title or the number of credit hours. As you saw above. I was hoping to use a regular expression to sort through the file and get each line that started with "CSC" or contained ".000 Course hours". I figured that after I got that output I could use a command in the sed to remove the new line from the end of the lines starting with the CSC and replace that with a comma. After that I would replace the backslash with a comma. However, to do that I think I would need to use an extended expression so sed would probably be out. The regular expression I was considering using is (^CSC |[0-9]\.000). So, should I be doing this in sed, awk, or perl. If you could please include your reasoning as to why it would be more efficient to use whatever method you suggest.
In Perl:
while (<>) {
chomp;
print if s/^CSC\s+/CSC/ and s/\s+-\s+/,/;
printf ",%.0f\n", $1 if /^([\d.]+)\s+Credit hours/;
}
I'd go with awk because you want to match and reformat lines and awk is perfect for this:
/CSC/ { # Lines that match CSC
split($0,a,"- ") # Split the line around the hyphen and following space
gsub(/ /,"",a[1]) # Remove the spaces from the first part of the split
printf a[1]","a[2] # Print the line in required format
}
/Credit hours/ { # Lines that match Credit hours
printf ",%i\n",$1 # Print the integer value of credit hours
}
Demo:
awk '/CSC/{split($0,a,"- ");gsub(/ /,"",a[1]);printf a[1]","a[2]}/Credit hours/{printf ",%i\n",$1}' file
CSC1010,COMPUTERS & APPLICATIONS,3
I prefer awk to Perl, which has no advantage (or disadvantage) for this. Using sed would be a regexp hack so I'd stay away from a sed solution.

Count the number of words for each contributor [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I count the number of words that each contributor wrote in a repository ?
It is best solved on a local clone of that GitHub, in order to analyze the git log and check each contributor.
See "getting contributor stats", based on:
git log --author="Jared Barboza" --pretty=tformat: --numstat | \
grep -v public/javascripts/jquery | \
gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END \
{ printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }' -
But that is for lines added/removed for a given contributor.
(a bit like in "Git: How to estimate a contribution of a person to my project in terms of added/changed lines of code?")
You can combined a similar approach with the one described in "Quantifying the amount of change in a git diff?".
Or you can use a dedicated program for that, like git-wordcount.

Convert a mixed string to UTF16 with PHP [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a string in PHP like this:
INPUT = "Γιώργο αν στείλεις αυτό ακριβώς (:
Its a mixed string with GREEK and LATIN characters at the same time. I want all characters in a given string to be converted to this string
OUTPUT = 0022039303B903CE03C103B303BF002003B103BD002003C303C403B503AF03BB03B503B903C2002003B103C503C403CC002003B103BA03C103B903B203CE03C200200028003A
How can I convert the input to this kind of output?
It took me a while to figure out but it seems that what you want to do is to convert a UTF-8 string to an uppercase binary dump of its UTF-16 representation. It's as simple as this:
$input = '"Γιώργο αν στείλεις αυτό ακριβώς (:';
$output = mb_strtoupper(bin2hex(mb_convert_encoding($input, 'UTF-16BE', 'UTF-8')));
The BE suffix indicates Big Endian, which seems to be the expected byte order according to your example.
Have you tried iconv? Maybe something like this:
(EDIT) i understand your main goal is to be able this converted string to be transported via http to your service, in that case you can use base64_encode() on the result.
see the complete code example:
<?php
$string = str_replace(' ', '', "Γιώργο αν στείλεις αυτό ακριβώς");
$converted = iconv('UTF-8', 'UTF-16', $string);
$encoded = base64_encode($converted);
var_dump($encoded);
var_dump(iconv('UTF-16', 'UTF-8', base64_decode($encoded)));
?>
You will get:
encoded: string '/v8DkwO5A84DwQOzA78DsQO9A8MDxAO1A68DuwO1A7kDwgOxA8UDxAPMA7EDugPBA7kDsgPOA8I=' (length=76)
decoded: string 'Γιώργοανστείλειςαυτόακριβώς' (length=54)