Joel Dare

Using AWK to Prepend or Append Data to Each Line

Awk is a tool that is included in Unix and most Unix variants such as Linux and Mac OS X. Using Awk, you can prepend or append data to the beginning or end of every line in a text file. Here’s an example:

cat input.csv | awk '{ print "12345," $0; }'

This command will take every line from input.csv, pipe it through Awk and echo it to stdout. Awk in turn, will print 12345, at the beginning of each of those lines. The $0 value means the rest of the line we passed to Awk from input.csv.

If you wanted to output that data to a new file, just use the typical greater than sign to redirect to a new file (output.csv in the following example).

cat input.csv | awk '{ print "12345," $0; }' > output.csv