This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
using_awk_to_prepend_or_append_data_to_each_line [2012/08/29 20:56] Joel Dare created |
using_awk_to_prepend_or_append_data_to_each_line [2023/05/22 12:51] Joel Dare |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 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//. | ||
+ | |||
+ | <box 50% round orange|Sweet Awk Magic>[[https://course.joeldare.com/awk/|Join my 90-minute workshop and learn my tips and tricks to quickly process CSV files with Awk.]]</box> | ||
+ | |||
+ | 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 | ||