one-liner

How can I use a Perl one-liner to decode an ASCII string encoded in hex?

I’d like to write a Perl one-liner to decode a line of ASCII characters encoded as hexadecimal numbers (for example the line 48 54 54 50 should be decoded as HTTP). I came up with this: perl -nE 'say map(chr, map { qq/0x$_/ } split)' It prints an empty line. What am I doing wrong and how would you write it? ...

What does a { at the end of a Perl one-liner mean?

Hello, I've seen this one-liner perl -lane '$_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{' file here: http://stackoverflow.com/questions/2311228/sum-up-different-row-values-in-a-file-using-sed-awk-perl and I don't remember how the "{" at the end works. Could someone explain how it works? ...

How can I write a leap-year program in one line using PHP?

How can I write a leap-year program in one line using PHP? ...

How would I remove all <script> tags (and everything in between) from multiple files using UNIX?

I have a folder with multiple files, and I'd like to remove all <script> tags and everything in between, e.g.: This: <script type="text/javascript">function(foo);</script> As well as this: <script type="text/javascript" src="scripts.js"></script> I think in PHP it would be something like this: <?php $string = preg_replace('#(\n?<...

Fastest/One-liner way to conditionally remove multiple directories on Unix

What is the shortest way I can write: rm -rfv public/stylesheets public/images public/javascripts and make it conditional, with something like: if [ ! -d public/stylesheets ]; then rm -rfv public/stylesheets; fi ... Just discovered/found a use for command-line conditionals :) ...

Why does my -p one-liner print the lines I'm trying to skip?

Hello! I have a file named file with three lines: line one line two line three When I do this: perl -ne 'print if /one/' file I get this output: line one when i try this: perl -pe 'next unless /one/' file the output is: line one line two line tree I expected the same output with bot...

How does the different behavior of the unless- and "if !" statement influence the range-operator in scalar context?

Hello! On http://sial.org/howto/perl/one-liner/ I found the following two one-liners. The outputs are different because the unless statement is different from if ! ( due to the associativity and precedence rules ). cat file: foo bar perl -ne 'print unless /^$/../^$/' file foo bar perl -ne 'print if ! /^$/../^$/' fi...

Fastest/One-liner way to list attr_accessors in Ruby?

What's the shortest, one-liner way to list all methods defined with attr_accessor? I would like to make it so, if I have a class MyBaseClass, anything that extends that, I can get the attr_accessor's defined in the subclasses. Something like this: class MyBaseClass < Hash def attributes # ?? end end class SubClass < MyBaseCla...

Cleanest/One-liner way to require all files in directory in Ruby?

When creating gems, I often have a directory structure like this: |--lib |-- helpers.rb `-- helpers |-- helper_a.rb `-- helper_b.rb Inside the helpers.rb, I'm just require-ing the files in the helpers directory. But I have to do things like this: $:.push(File.dirname(__FILE__) + '/helpers') require 'helper_a'...

How can I call a module in a Perl one-liner?

Say I have a data file that I want to process; I want to take the maximum value of each of the column and append it to the end of each line. INPUT: T1 T2 T3 35.82 34.67 31.68 32.20 34.52 33.59 37.41 38.64 37.56 OUTPUT: T1 T2 T3 35.82 34.67 31.68 35.82 32.20 34.52 33.59 34.52 37.41 38.64 37.56 38.64 I'm trying to implement this as ...

Why do I have to specify the -i switch with a backup extension when using ActivePerl?

I cannot get in-place editing Perl one-liners running under ActivePerl to work unless I specify them with a backup extension: C:\> perl -i -ape "splice (@F, 2, 0, q(inserted text)); $_ = qq(@F\n);" file1.txt Can't do inplace edit without backup. The same command with -i.bak or -i.orig works a treat but creates an unwanted backup file ...

One-liner to Convert Nested Hashes into dot-separated Strings in Ruby?

What's the simplest method to convert YAML to dot-separated strings in Ruby? So this: root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: K To this: { "ROOT.CHILD_A" => "Hello", "ROOT.CHILD_B.NESTED_CHILD_A" => "Nesting", "ROOT.CHILD_B.NESTED_CHILD_B" => "Nesting Again", ...

How to create list of numbers and append its reverse to it efficiently in Ruby

Given a minimum integer and maximum integer, I want to create an array which counts from the minimum to the maximum by two, then back down (again by two, repeating the maximum number). For example, if the minimum number is 1 and the maximum is 9, I want [1, 3, 5, 7, 9, 9, 7, 5, 3, 1]. I'm trying to be as concise as possible, which is w...

python histogram one-liner

there are many ways, how to code histogram in Python. by histogram, i mean function, counting objects in an interable, resulting in the count table (i.e. dict). e.g.: >>> L = 'abracadabra' >>> histogram(L) {'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2} it can be written like this: def histogram(L): d = {} for x in L: if x ...

Eclipse formatter to keep One-Liners.

Can Eclipse Formatter be configured to keep: public Long getId() { return this.id; } And maybe to format small (one line) definitions as one-liners? ...

One liner in Ruby for displaying a prompt, getting input, and assigning to a variable?

Often I find myself doing the following: print "Input text: " input = gets.strip Is there a graceful way to do this in one line? Something like: puts "Input text: #{input = gets.strip}" The problem with this is that it waits for the input before displaying the prompt. Any ideas? ...

Wrapping of comma separated data in bash to a fixed line length

Hi I would like to wrap the following comma separated data: -X, run, abs, absolute, accept, accept, alarm, schedule, atan2, arctangent, bind, binds, binmode, prepare, bless, create, caller, get, chdir, change, chmod, changes, chomp, remove, chop, remove, chown, change, chr, get, chroot, make, close, close, closedir, close, connect, conn...

Powershell One-Liner to return version of .NET Framework on a machine?

My first guess is something involving WMI. Does anybody know better? Clarification: One-Liner to return only the latest version for each installation of .NET ...

python one-liner

Hello, I want a one-liner solution In Python of the following code but how? total = 0 for ob in self.oblist: total+=sum(v.amount for v in ob.anoutherob) It returns total value. I want it one liner , plz any one help me ...

Csh alias with perl one-liner evaluates when alias is created and not when alias is used

I know, I know, I should use a modern shell... Anyway, so here is the alias: alias p4client echo `p4 info | perl -ne 's/Client name: (.*)$/print $1/e'` I also know there is probably a better way to get the p4 client name. This is just an example. So, can anyone tell me a nice and clean way to get this to evaluate this when I invoke...