Hot answers tagged perl
15
That's for compatibility with the Bourne shell. The Bourne shell was an old shell that was first released with Unix version 7 in 1979 and was still common until the mid 90s as /bin/sh on most commercial Unices.
It is the ancestor of most Bourne-like shells like ksh, bash or zsh.
It had a few awkward features many of which have been fixed in ksh and the ...
13
In Larry Wall's original Perl v1.0 posting to the comp.sources.misc newsgroup on December 18, 1987, he said:
If you have a
problem that would ordinarily use sed or awk or sh, but it exceeds
their capabilities or must run a little faster, and you don't want
to write the silly thing in C, then perl may be for you.
In a much later ...
11
The answer is/isn't sexy, depending on your point of view.
Perl is very useful. Lots of the system utilities are written in or depend on perl. Most systems won't operate properly if Perl is uninstalled.
A few years ago FreeBSD went through a lot of effort to remove Perl as a dependency for the base system. It wasn't an easy task.
10
You can use grep for that
grep -vh '^[[:space:]]*#' filename
Since, as I presume, you are stripping comments from some file, you might also consider removing empty lines, which expands the above to:
grep -vh '^[[:space:]]*\(#\|$\)' filename
9
You can't.
The Perl script runs in a process which is a child of your shell session. This child process can change its own working directory all it likes, but it cannot change it's parent's working directory. When the Perl script exits, control is returned to the parent process (the shell session), which will have remained in the same working directory ...
8
awk -v 'OFS=\t' 'NF == 2 { print $1, "none", $2; next } 1' input.txt > output.txt
Adjust depending on the characteristics of your input file. I assume every line with only 2 fields should have a "none" inserted. Otherwise, all other lines are just passed through unchanged (the purpose of the 1 at the end).
8
To skip every other > ... block:
awk '/^>/ { p = !p } p' input.txt
p is a print flag - p means print when p is true, as the default action is print when none is provided. In awk, variables start out empty, which evaluates to false in Boolean contexts. Every time a > ... line is reached, toggle the p flag.
To print every nth block delimited by ...
7
If you just want to write a single y to the stdin of the process, you can do this:
(echo y | nohup myprocess) &
If you want to keep writing y for every prompt that comes up, the coreutil yes exists for exactly this purpose -- it will keep writing whatever you tell it to to stdout. Its default is to output "y", so you can just:
(yes | nohup myprocess) ...
6
One moderately ugly way to do it is
grep -v pattern file >file.tmp; diff -c file.tmp file
or replace -c with -C NUM for NUM lines of context. It'll produce extra output, though. (If your diff supports -u/-U NUM, it'll be cleaner.)
If your diff doesn't have -c/-C/-u, there are still ways to do it, but they're pretty ugly. On the other hand, a system ...
6
sed has a function for that, and can do the modification inline:
sed -i -e '/Pointer/r file1' file2
But this puts your Pointer line above the file1. To put it below, delay line output:
sed -n -i -e '/Pointer/r file1' -e 1x -e '2,${x;p}' -e '${x;p}' file2
6
Update: fixed a typo in the script: changed print $NF to print $3; also tidied things up, and added some comments.
Assuming file names do not contain \n, the following prints out a sorted list which breaks (as in: section control breaks) at unique file name, unique md5sum, and shows the corresponding group of file paths.
#!/bin/bash
# Choose which ...
6
You want the range operator. You don't need to "slurp" your file either.
STR='sla\ndfjAAAAk\nsaljdfla\nsjdfBBBBsadf\njasdf'
echo -e ${STR} | perl -nE 'm{^dfjAAAAk}..m{^sjdfBBBB} and print'
dfjAAAAk
saljdfla
sjdfBBBBsadf
5
I prefer gawk for this:
awk -vOFS='\t' 'NF{$1=FILENAME OFS $1;$2=strftime("%c",$2)}1' filename.txt
Here is one perl alternative too:
perl -nae 'print$ARGV,"\t",$F[0],"\t".localtime($F[1]),"\n"' filename.txt
As you also asked about bash, here is what it could do:
while read -r who when; do
readlink -n /proc/$$/fd/0
echo -en "\t$who\t"
date -d ...
5
This simple perl script emulates grep -A to some extent
#!/usr/bin/perl
$pattern=shift; #patthern to search
$lines=shift; # number of lines to print
$n = 0;
while (<>) {
$n = $lines if /$pattern/; # reset counting
if ($n) { print; $n-- } # print if within
$n = 0 if eof; # don't leak across file boundaries
}
Note that you may add a usage ...
5
I wonder whether the small sponge general-purpose utility ("soak up standard input and write to a file") from moreutils will be helpful in this case and whether it will follow the symlink.
The author describes sponge like this:
It addresses the problem of editing
files in-place with Unix tools, namely
that if you just redirect output to
the file ...
5
Since you posted this on the Unix & Linux site, instead of StackOverflow, I'm wondering if something like this isn't what you're looking for:
cat hostlist.txt | xargs resolveip -s
This will only return one IP address though.
Some hostnames will have multiple IP addresses associated with them:
$ host www.google.com
www.google.com is an alias for ...
5
There are several methods described in the Arch wiki. The easiest version is probably to use perl-cpanplus-dist-arch, which, after installing and running the setup with setupdistarch can install perl modules as pacman packages with cpanp -i Your::Module::Name as described in its documentation.
5
Try this:
sed 's/=[ \t]*/=/' input.txt
Specify an -i option for in-place edit if you want.
sed -i not fit for solaris
Either redirect to another file and rename to original or using perl instead:
perl -pi -e 's/=[ \t]*/=/g' input.txt
5
You seem to be confusing bash with perl. The default shell on your mac is bash, which cannot set variables in the same way, or using the same syntax, as perl. To set a variable called '$number' to the result of '13 * 2' using perl, on a bash CLI, you would:
my-macbook:~ $ perl -e '$number = 13 * 2; print $number, "\n";'
26
To do the same thing in bash ...
5
Using awk :
The input file
$ cat FILE
Id ht
510 69
510 67
510 65
510 62
510 59
601 29
601 26
601 21
601 20
Awk in a shell :
$ awk '
NR>1{
arr[$1] += $2
count[$1] += 1
}
END{
for (a in arr) {
print "id avg " a " = " arr[a] / count[a]
}
}
' FILE
Or with Perl in a shell :
$ perl -lane '
...
5
You could do it like this with awk:
awk 'BEGIN { FS = OFS = "\t" } { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = 0 }; 1' file
Explanation
Setting FS and OFS to tab ensures the output is correctly delimited. The for-loop looks at every field and set it to zero if they're empty. The one at the end is a shorthand for { print $0 }.
5
Because you use single quotes instead of double...
Perl doesn't interpolate variables enclosed in single quotes, so what you are doing is sending the string '$srceDir' to the shell which will normally be unset (blank) unless you have it set in your environment somewhere.
Try this:
my $find_cmd = "find $srceDir -type f -newermt 2013-02-14 ! -newermt ...
4
If you're running a sendmail with smrsh set up (common in a lot of default configurations) you will need to run the piped command out of /etc/smrsh/. It can either be a symlink or a copy of the script, but if sendmail has 'smrsh' defined, it will need to be run from that directory. For example:
subscribe: | /etc/smrsh/parse-subscribe.pl
Check the ...
4
EDIT .. re your comment.. Here is a new script which doesn't concern itself about (eg.) William Smith. It temporarily obfuscates patterns which it keeps as Smith (unchanged).
sed -r 's/\<(John) (Smith)\>/\1\x01x\2/g;
s/\<Smith\>/John/g; s/\x01x/ /g'
If you are concerned about Mr. Mr Mrs... then this works.
sed -r ...
4
There are several usual notations for IPv4 and IPv6 addresses. Here's an extended regular expression, suitable for Perl m//x, that captures the usual notations. If you remove the comments and whitespace, you can use it with grep -E, awk, or any other utility that uses extended regular expressions (ERE).
^(
...
4
That's the Perl-based rename found on Debian, Ubuntu and derivatives, judging by the syntax. You can't use the tr operator because it acts on the whole string. But you can match the extension, and lowercase it with \L.
rename 's/\.([^.]*$)/.\L$1/' *.MOV
For the zsh fans:
autoload zmv
zmv '(*.MOV)' '${1:r}.${(L)1:e}' # current directory only
zmv ...
4
A little terrible sed oneliner:
sed -n \
# we divide out incoming text to small parts,
# each one as you mentioned from /---.*box.*/ to /profile/
'/---.*box.*/,/profile/{
# inside of each part we do following things:
# if string matches our pattern we extract
# the value and give it some identifier (which you
# can see is "ij", "st" ...
4
One way using perl assuming infile has content of your question (IDs won't be necessarily in same order in output because I use a hash to save them):
Content of script.pl:
use strict;
use warnings;
my (%data);
while ( <> ) {
## Omit header.
next if $. == 1;
## Remove last '\n'.
chomp;
## Split line in spaces.
my @f = ...
4
Like this, but it doesn't include the header line:
$ awk '{print $1}' file2.txt | grep -vf - file1.txt
44888 56565 45554 6868
77765 88688 87464 6848
Note: I adjusted this to match the example output, not your description. If you want it the other way just switch file1 and file2.
Breaking this down:
awk prints just field 1 from file2.txt
grep -v inverts ...
4
Using Python (2.7) with standard modules:
file test.xml:
<Container>
<Placemark>
<KeepMe/>
</Placemark>
<Placemark>
<styleUrl>#m_ylw-pushpin330</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
0.0000000000000,0.0000000000000,0 ...
Only top voted, non community-wiki answers of a minimum length are eligible

