<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_definitions}
\input{common_latex/common_latex_help_format}
%===================================================================

\begin{document}

\begin{labcover}
\labtitle{Help Topic: Basic awk Usage}
\vspace{0.1in}
\labdate{Spring 2020}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labline
\end{labcover}

\wikidoc{file=Help.CMDAwk}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{Using awk}
%====================================================================
\label{sec_help_awk}

%====================================================================
\subsection*{Basic \cmd{awk} usage}
\label{sec_help_basic_awk}

\cmd{awk} is a powerful command line tool for manipulating
files. \cmd{awk} is specified by POSIX, so if you have a Unix based
OS, you have \cmd{awk}. You can check:

\begin{consoleverb}
which awk

/usr/bin/awk
\end{consoleverb}

\cmd{awk} operates over the lines of its input (either a file or stdin). A
useful \cmd{awk} command is \texttt{print}:

\begin{consoleverb}
awk 'END {print "Hello, world"}'
\end{consoleverb}

\noindent
This \cmd{awk} program does nothing until reaching the \texttt{END} of its input.
Since no file is provided, \cmd{awk} is reading from stdin and will do
nothing until you signal EOF with Ctrl-D.

\begin{consoleverb}
Hello, world
\end{consoleverb}

Let's look at how \cmd{awk} works when it's actually reading a file:

\begin{consoleverb}
printf "first line\nsecond line" &gt; foo.txt
awk '{print $0}' foo.txt

first line
second line
\end{consoleverb}

\noindent
The \$0 macro means "the entire line". Alternatively, you an access
"fields" using \$1, \$2, etc. \cmd{awk} separates the input into fields based
on the value of the variable \texttt{FS}. The default value of \texttt{FS} is a
space.

\begin{consoleverb}
awk '{print $2, $1}' foo.txt

line first
line second
\end{consoleverb}

%====================================================================
\subsection*{Extracting Data with awk}
\label{sec:help_extract_awk}

The \cmd{awk} primary utility in MOOS is to extract logged data from an alog
file. \app{aloggrep} extracts individual variables from the file, but
keeps additional information that interferes with piping the logged
values to other programs.

\begin{consoleverb}
aloggrep -q short.alog NAV_X

350.163         NAV_X                iM200           130.35441    
350.363         NAV_X                iM200           130.41986    
350.666         NAV_X                iM200           130.48531    
350.967         NAV_X                iM200           130.64412    
351.168         NAV_X                iM200           130.70957    
351.348         NAV_X                iM200           130.74666    
351.586         NAV_X                iM200           130.79748    
351.973         NAV_X                iM200           130.89911    
352.175         NAV_X                iM200           130.92202    
352.376         NAV_X                iM200           130.93121    
352.679         NAV_X                iM200           130.93994    
352.982         NAV_X                iM200           130.95740
\end{consoleverb}

The second and third columns (the variable and the publishing App) are
not desired (and time in the first column may not be desired either).
To export the x position against time to other utilities, we can pipe
aloggrep to \cmd{awk}:

\begin{consoleverb}
aloggrep -q short.alog NAV_X|awk '{print $1, $4}'

350.163 130.35441
350.363 130.41986
350.666 130.48531
350.967 130.64412
351.168 130.70957
351.348 130.74666
351.586 130.79748
351.973 130.89911
352.175 130.92202
352.376 130.93121
352.679 130.93994
352.982 130.95740
\end{consoleverb}

%====================================================================
\section*{Using gnuplot}
%====================================================================
\wikisec{file=gnuplot}
\label{sec:orgheadline1}

\cmd{gnuplot} is a command line plotting program. It should be available
through your pacakge manager. More information can be found at the
homepage, \var{http://www.gnuplot.info/}.

Let's try a simple gnuplot example. First, create a dummy file, then
invoke gnuplot interactively.

\begin{consoleverb}
printf "0 0\n1 1\n2 2\n3 3\n" &gt; foo.dat
gnuplot
\end{consoleverb}

From the interactive \cmd{gnuplot} prompt, you can invoke any \cmd{gnuplot}
command. This basic plot command will plot the data we stored in \texttt{foo.dat}.

\begin{consoleverb}
plot 'foo.dat'
\end{consoleverb}

\includegraphics[width=.8\linewidth]{figs_help/basic_gnuplot.png}

Now close the plot and quit \cmd{gnuplot} with \texttt{Ctrl-D}. 

\cmd{gnuplot} will accept data on stdin when told to plot '\texttt{-}'.

\begin{consoleverb}
gnuplot -e "plot '-'"
\end{consoleverb}

\noindent
Now type the same data (0 0 \ldots{} 3 3) followed by \texttt{e} (when stdin is a
tty instead of a pipeline, \cmd{gnuplot} keeps reading from stdin until
it sees \texttt{e}).

\begin{consoleverb}
0 0
1 1
2 2
3 3
e
\end{consoleverb}


%===================================================================
\subsection*{Combining \app{aloggrep}, \cmd{awk}, and \cmd{gnuplot}}
\label{sec_help_comb_ag_awk_gp}

The ability to plot from the command line makes it very easy to
visualize alog data. Lets plot the X data that we extracted from
aloggrep using \cmd{awk} before.

\begin{consoleverb}
aloggrep -q short.alog NAV_X|awk '{print $1, $4}'|gnuplot -e "plot '-'"
\end{consoleverb}

\includegraphics[width=.8\linewidth]{figs_help/gnuplot_pipe.png}

\end{document}

</pre></body></html>