one-liner

How does this Perl one liner to check if a directory is empty work?

I got this strange line of code today, it tells me 'empty' or 'not empty' depending on whether the CWD has any items (other than . and ..) in it. I want to know how it works because it makes no sense to me. perl -le 'print+(q=not =)[2==(()=<.* *>)].empty' The bit I am interested in is <.* *>. I don't understand how it gets the names ...

How to insert a new line character after a fixed number of characters in a file

I am looking for a bash or sed script (preferably a one-liner) with which I can insert a new line character after a fixed number of characters in huge text file. ...

excluding first and last lines from sed /START/,/END/

Consider the input: =sec1= some-line some-other-line foo bar=baz =sec2= c=baz If I wish to process only =sec1= I can for example comment out the section by: sed -e '/=sec1=/,/=[a-z]*=/s:^:#:' < input ... well, almost. This will comment the lines including "=sec1=" and "=sec2=" lines, and the result will be something like: #=sec...

Oneliner to count the number of tabs in each line of a file

I have a file that is tab delimited. I would like a powershell script that counts the number of tabs in each line. I came up with this: ${C:\tabfile.txt} |% {$_} | Select-String \t | Measure-Object | fl count it yields 3, Which is the number of lines in the file. any pointers to what I'm doing wrong? I would like it to print a sin...

How can I print the names of the files being processed in a Perl one-liner?

I generally use perl one liners instead of grep to search through files. For example following prints all the locations containing #include <stdio.h> perl -ne "print if(/#include\s*[\"<]stdio.h/)" */*.[ch] But I could not find a way to print the filename that got these lines. I tried to print $ARGV[0] but no avail. So, how do you pr...

version of ldap installed - one liner

HI, I am using LDAP which is installed in a solaris machine. To check the version of LDAP i go to /ldap and check the version installed as if it is version 5 then there is a directory of the name v5.0 and so on. After getting into the directory i check the directory structure. Can anybody tell me is there any shortest way or one liner t...

In Python, how do I create a string of n characters in one line of code?

I need to generate a string with n characters in Python. Is there a one line answer to achieve this with the existing Python library? For instance, I need a string of 10 letters: string_val = 'abcdefghij' ...

One-liner to convert two newlines to one?

I have a file where I want to convert "\n" to " " and "\n\n" to "\n". For example: I had a toy . It was good . Becomes: I had a toy . It was good . Does anyone have a Unix one-liner for this operation? ...

Fastest/One-liner way to collect duplicates in Ruby Array?

What's the fastest/one-liner way to convert an array like this: [1, 1, 1, 1, 2, 2, 3, 5, 5, 5, 8, 13, 21, 21, 21] ...into an array of objects like this: [{1 => 4}, {2 => 2}, {3 => 1}, {5 => 3}, {8 => 1}, {13 => 1}, {21 => 3}] ...

Is there a way to treat Ping-Host as a boolean in PowerShell?

I'd like to do something like this: if (Ping-Host server1) { blah } Anyone know of a simple way? ...

PowerShell query: how to process a file

I often want to process a file resulting in a modified output file. I can't seem to find the PowerShell way to do this - it always ends up looking like code in a one-liner, IYSWIM. This is an example. I have an LMHOSTS file. Don't ask why! I want to get a new LMHOSTS file that only contains servers that respond to pings. I also wan...

Why does my perl one-liner ignore the first line of input?

I'm trying to strip some content from an HTML file automatically, and I'm using the following command to strip everything up to the useful data: perl -pi.bak -e 'undef $/; s/^.*?<pre>//s' $file However, for some reason this leaves the first line of the HTML file (the DOCTYPE declaration) alone. ...

Executing for-each in bash

I'm looking to write a Bash one-liner that calls a function once for each item in a list. For example, given the list foo bar baz and the program "cowsay", it would produce: _____ < foo > ----- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || _____ < bar > ---...

Combine Thumbnails to One Large Image with RMagick

What's the shortest way to combine say 20 256x256 thumbnails into a single large image of 4 rows by 5 columns using RMagick? ...

Fastest/One-liner way to get XML nodes into array of "path/to/nodes" in Ruby?

What is the fastest, one-liner/shortest way to get an Array of "strings/that/are/paths" from an XML file, using Nokogiri preferably. I'd like to build the array with an arbitrary attribute name ('id' in this case), but also knowing how to do it for the element name would be helpful. So this: <root id="top"> <nodeA id="almost_top"...

Fastest/One-liner way to print XML node's XPath in Ruby?

What's the fastest/one-liner way to print the current nodes xpath, or just "path/to/node", in Ruby with Nokogiri? So this: <nodeA> <nodeB> <nodeC/> </nodeB> </nodeA> to this (say we've gone down to nodeC by processing xml.children.each, etc...): "nodeA/nodeB/nodeC" ...

Iteration in a single line

I have some code of the form: for i in range(nIterations): y = f(y) Where f is a function defined elsewhere. hopefully the idea of that code is that after it's run y will have had f applied to it nIterations times. Is there a way in python to write this in a single line? ...

Setting numpy slice in lambda function

I want to create a lambda function that takes two numpy arrays and sets a slice of the first to the second and returns the newly set numpy array. Considering you can't assign things in lambda functions is there a way to do something similar to this? The context of this is that I want to set the centre of a zeros array to another array ...

What's the easiest way to convert a list of integers to a string of comma separated numbers in C#

I'm looking for the one liner here, starting with: int [] a = {1, 2, 3}; List<int> l = new List<int>(a); and ending up with String s = "1,2,3"; ...

Rails 'returning' method - One-liner to build Array from Multiple Arrays in Ruby?

How do I convert the following to a clear one-liner in ruby? def questions results = [] sections.each do |section| results = results.concat(Question.find_all_by_survey_section_id(section.id)) end results end I feel like I could use the &: (Symbol#to_proc) and returning methods in there somehow. ...