Questions tagged [one-liner]

One Liners are an abridged one-line version of a multi-line script.

Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The one-liner can be

  • an expression written in the language of the shell;
  • the invocation of an interpreter together with program source for the interpreter to run;
  • the invocation of a compiler together with source to compile and instructions for executing the compiled program.

Certain dynamic scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners.

The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

Find more information at:

153 questions
1
vote
1 answer

Prevent final character from being a number

In bash, I run the following to get a random string: LC_CTYPE=C tr -dc 'a-z0-9'
KolonUK
  • 443
  • 1
  • 6
  • 14
1
vote
1 answer

Powershell not converting Foreach-Object parameter from string to integer even when provided [Int32] for type-casting

As mentioned in Question title, I have a Powershell one-liner for killing the Application/Process by Name and all it's relevant children processes: powershell -c "Get-Process -Name `"Chrome`" | Select-Object -Property Id | ForEach-Object -Process {…
Vicky Dev
  • 1,893
  • 2
  • 27
  • 62
1
vote
2 answers

Counting all characters except space in the using the ls|wc-c?

Suppose running ls command gives the following output on the terminal: 1.txt 2.txt 3.txt '4 b' ['4 b' is the name of a folder] When I run ls | wc -c at the same location, I get 18, which is because it considers space as a character. What should I…
1
vote
1 answer

Need windows batch command one-liner to remove folders by name, not by date.time using powershell if applicable

Need help with command like, one-liner, powershell to remove folders I'm trying to find an elegant way to remove folders by folder name which reflects the date but I cannot rely on the file/folder date meta-data attributes. Here's the problem I'm…
James
  • 63
  • 9
1
vote
2 answers

Printing filename for a multi-line pattern found in multiple files

I am doing perl -pe along with grep to do a multi line grep. This is being done so that when "" is used as a line continuation letter, I need to join the line. So my file is record -field X \ -field Y I am doing perl -pe 's/\\\n/ /'…
TKH
  • 119
  • 3
1
vote
1 answer

Perl one liner to seek to offset in file

Is there a way to specify reading in file bytes that are at a position/offset within a binary file from the cmdline? (i.e. one-liners) e.g. perl -ne <...seek n bytes into file... do stuff...> inputfile I haven't seen this and have tried using seek,…
skyfire
  • 227
  • 1
  • 6
1
vote
1 answer

Conditional editing of line by one-liner

My question is more of an optimization one, rather then a "howto". I have a lef file, with thousands of lines in the form of: RECT 429.336 273.821 426.246 274.721 ; I wanted to move left by 4 um all rects above a certain point using a…
user2141046
  • 862
  • 2
  • 7
  • 21
1
vote
1 answer

Using xargs parameterrs as variables to compare two md5sum

I'm extracting two md5sums by using this code: md5sum test{1,2} | cut -d' ' -f1-2 I'm receiving two md5sums as in example below: 02eace9cb4b99519b49d50b3e44ecebc d8e8fca2dc0f896fd7cb4cb0031ba249 Afterwards I'm not sure how to compare them. I…
c0d33p
  • 11
  • 1
1
vote
2 answers

What's the idiomatic way to get the last element from a generated array?

I want to get the last element from a generated array in a single line, is there a better way to do it? I was thinking assign the array to foo then immediatly assign the last element of foo to itself again but it doesn't work: function* getArray()…
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
1
vote
0 answers

Solving some Python regex tasks in one line

I have to solve some Python regex tasks in one line of code. An example of such a task would be this one: print (one of) the longest word in a text. Here is how I did it: import re print(sorted([(word, len(word)) for word in list(filter(None,…
Alexdanut
  • 111
  • 6
1
vote
1 answer

Perl one liner in Bash script

I have a bash script that runs, and I'm trying to use a Perl one-liner to replace some text in a file variables.php However, I would like to check if the Perl one-liner runs successfully and that's where I get hung up. I could just output the…
John David
  • 388
  • 1
  • 4
  • 18
1
vote
2 answers

Linux string manipulation to make two parameters from one

I'm receiving a list of operating systems, one per line, such as: alpine/3.13 alpine/edge alt/Sisyphus alt/p9 apertis/v2019.5 I need to massage each line into two parameters to yield: lxc launch images:alpine/3.13 alpine-3-13 lxc launch…
Pascal
  • 267
  • 2
  • 10
1
vote
1 answer

Simplify array processing and enumerate to a one-liner

How can I simplify the following: imgs = [] for i, obj in enumerate(objlist): imgs.append( foo(obj, f"img-{i}.png") ) where objlist an array of objects and foo is a method that processes the object and saves an image receiving both the object…
Diego Vallejo
  • 55
  • 1
  • 7
1
vote
2 answers

Using perl to round number

I am having a file with numbers in MB units and I wanted to make them in GB then I found a very interesting perl to do this perl -pe 's{(?
mshafey
  • 89
  • 1
  • 9
1
vote
1 answer

List Comprehension Hackerrank - can you tell me if this one liner is permittable to use?

I was currently trying to solve this challenge - List Comprehensions - and came up with this solution in python3: if __name__ == '__main__': x = int(input()) y = int(input()) z = int(input()) n = int(input()) list = [] for a in…