Links | Resources

Resources: Miscellaneous

  • Histogram: histogram.awk
    Produces a histogram. Input file contains a single column of values. To bin data with data values between -xmin- and -xmax-, with -nbin- bins. Usage: gawk -f histogram.awk xmin xmax nbin data.out

  • Linear Regression: linreg.awk
    Given containing two columns x and y, outputs a=slope and b=intercept. Usage: awk -f linreg.awk

  • Extract internal segment: 5 7, outputs lines 5-7 (3 lines) of the file

  • Statistics: statistics.awk
    awk -f stat.awk data.n, outputs the arithmetic mean, standard deviation and standard error of column 2.

  • Append columns from a different file:
    To append column "n" from file2 to file1, file1 file2 n

  • Add two tables:
    Given two identically structured files (# columns, and lines) adds up respective elements. Right now assumes all rows have the same number of columns. Usage: sh f1 f2 ncol

  • Big-small endian conversion:
    DOS to Unix: awk '{sub(/\r$/,"");print}'
    Unix to DOS: awk '{sub(/$/,"\r");print}'

  • Substitution and Find:
    (From Eric Pemente:
    # substitute "foo" with "bar" ONLY for lines which contain "baz"
    awk '/baz/{gsub(/foo/, "bar")};{print}'
    # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
    awk '!/baz/{gsub(/foo/, "bar")};{print}'
    # change "scarlet" or "ruby" or "puce" to "red"
    awk '{gsub(/scarlet|ruby|puce/, "red"); print}'
    # print section of file between two regular expressions (inclusive)
    awk '/Iowa/,/Montana/' # case sensitive

  • Join PDFs using Ghostscript:
    Joins two or more files into a single PDF file called JoinedFiles.pdf
    Usage: sh f1.pdf f2.pdf f3.pdf
    Globbing is allowed: sh f*.pdf

  • Text to LaTeX columns: txt2textable.awk
    Given text file with arbitrary number of columns, outputs in latex format.

© 2008 Sachin Shanbhag
Last Modified: 10/21/2008