User Tools

Site Tools


apply_unix_commands_to_all_but_the_first_line

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
apply_unix_commands_to_all_but_the_first_line [2022/08/11 22:00] – created rajuapply_unix_commands_to_all_but_the_first_line [2022/08/12 15:16] – [How I came across it] raju
Line 69: Line 69:
 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). 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 that entire repository and add it to your PATH.+A more practical approach is to clone that entire repository and add it to the shell'PATH.
  
 I did it as follows. I did it as follows.
  
-Remove the bare bones script we added in the previous step+Remove the bare bones script added in the previous step
 <code> <code>
 % rm ~/bin/body % rm ~/bin/body
Line 101: Line 101:
 </code> </code>
  
-Open a new bash session and verify that these utilities are correctly picked up.+Open a new bash session and verify that the scripts are picked up from the correct location.
 <code> <code>
 % which body % which body
Line 110: Line 110:
 </code> </code>
  
-Verify that the utilities are working as expected.+Verify that the scripts are working as expected.
 <code> <code>
 % echo -e "value\n8\n2\n6\n3" | body sort % echo -e "value\n8\n2\n6\n3" | body sort
Line 130: Line 130:
 ==== How I came across it ==== ==== How I came across it ====
 I came across it while reading the book "Data Science at the Command Line" (2nd Edition) by Jeroen Janssens (https://smile.amazon.com/Data-Science-Command-Line-Explore/dp/1492087912). The book is available for free at https://datascienceatthecommandline.com/2e/ . The 'body' and 'header' commands are discussed in https://datascienceatthecommandline.com/2e/chapter-5-scrubbing-data.html#bodies-and-headers-and-columns-oh-my . I came across it while reading the book "Data Science at the Command Line" (2nd Edition) by Jeroen Janssens (https://smile.amazon.com/Data-Science-Command-Line-Explore/dp/1492087912). The book is available for free at https://datascienceatthecommandline.com/2e/ . The 'body' and 'header' commands are discussed in https://datascienceatthecommandline.com/2e/chapter-5-scrubbing-data.html#bodies-and-headers-and-columns-oh-my .
 +
 +==== Close but no cigar ====
 +Here I will try to list some alternative approaches that are close but not perfect.
 +
 +Solution 1:
 +<code>
 +$ cat input.txt
 +value
 +8
 +2
 +6
 +3
 +
 +$ (head -n 1 input.txt; tail -n +2 input.txt | sort)
 +value
 +2
 +3
 +6
 +8
 +</code>
 +
 +Disadvantages:
 +  * Requires two passes on the input (once to extract the header and another to process the body)
 +  * Does not work if the input is coming from stdin
  
apply_unix_commands_to_all_but_the_first_line.txt · Last modified: 2022/08/12 17:29 by raju