​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking


The SED I'm using was a port to OpenVMS hence the double quotes used.

1)  Reverse the lines of a text file

sed "1!G;h;$!d" filename

No I wasn't blind-folded when I typed that command!

2) Number a file

sed "=" filename | sed 'N;s/\n/\t/'

3) Double a space a file 

sed G filename

4) Count the lines in a file

sed -n "$=" filename

5) Output the first X lines of a file

sed Xq filename

6) Print the last line only

sed -n "$p"

7) Print one line of context before and after a regexp and line num where regexp occurred

sed -n -e "/regexp/{=;x;1!p;g;$!N;p;D;}" -e h filename

8) Starting at line 3 delete every 6 lines

sed -n "3,${p;n;n;n;n;n;n;}" filename
9) print a section of file between two regular expressions (inclusive)

sed -n "/Iowa/,/Montana/p" filename

10) print all of file EXCEPT section between 2 regular expressions

sed "/Iowa/,/Montana/d" filename
11) delete duplicate, consecutive lines from a file (emulates "uniq").
First line in a set of duplicate lines is kept, rest are deleted.

​sed "$!N; /^\(.*\)\n\1$/!P; D" filename

12) delete the last X lines of a file

sed -n -e :a -e "1,X!{P;N;D;};N;ba" filename

Some useful SED commands