User Tools

Site Tools


task_boiler

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
task_boiler [2022/08/11 21:29] rajutask_boiler [2024/01/23 22:55] (current) raju
Line 3: Line 3:
  
 Start the first heading with "=====" Start the first heading with "====="
- 
-===== apply unix commands to all but the first line ===== 
-==== Situation ==== 
-Let's say we want to sort a series of numbers in descending order but keep the header at the top. For example, given 
-<code> 
-$ echo -e "value\n8\n2\n6\n3" 
-value 
-8 
-2 
-6 
-3 
-</code> 
-we want to output 
-<code> 
-value 
-2 
-3 
-6 
-8 
-</code> 
- 
-We can't directly use 'sort' since it will sort the header as well. 
-<code> 
-$ echo -e "value\n8\n2\n6\n3" | sort 
-2 
-3 
-6 
-8 
-value 
-</code> 
- 
-==== bare bones solution ==== 
-Create a script called 'body' with the following contents 
-<code> 
-#!/usr/bin/env bash 
-# 
-# body: apply expression to all but the first line. 
-# Use multiple times in case the header spans more than one line. 
-# 
-# Example usage: 
-# $ echo -e "value\n8\n2\n6\n3" | body sort 
-# 
-IFS= read -r header 
-printf '%s\n' "$header" 
-"$@" 
-</code> 
- 
-Make it executable 
-<code> 
-chmod +x body 
-</code> 
- 
-place it somewhere in your PATH (say ~/bin) 
-<code> 
-mv body ~/bin 
-</code> 
- 
-This script will apply any unix command to all but the first line. For example, using it on our example 
- 
-<code> 
-$ echo -e "value\n8\n2\n6\n3" | body sort 
-value 
-2 
-3 
-6 
-8 
-</code> 
- 
-==== practical solution ==== 
-I got the above script from https://github.com/jeroenjanssens/dsutils/blob/master/body . The underlying repository (https://github.com/jeroenjanssens/dsutils) contains many such useful scripts (ex:- header - to add, replace, and delete header lines). A more practical approach is to clone the entire repository and add the repo location to your PATH. 
  
task_boiler.1660253352.txt.gz · Last modified: 2022/08/11 21:29 by raju