How do I create a bash script to duplicate a file, swap first two columns and append to bottom of original file?

Issue

I would like to write a bash script to duplicate the product-feed swapping the first two columns(sku, productId) and append it to feed. This is what I have so far but it does not seem to be working.

  1. Duplicate the feed
  2. Swap the first two columns
  3. Append it to the original feed

1-Duplicate the feed–> cd /var/ftp/JNM-01-020420/inbound/product-feed/en_US && cp ./*.csv /var/ftp/JNM-01-020420/inbound/product-feed/en_US/tmp

2-Swap Columns–> awk '{t=$1; $1=$2; $2=t; print;}' ./tmp

3-Append to original feed –> ./tmp >> ./*.csv

Example of product feed for reference

Solution

Tested on 150 MB file:

awk 'BEGIN {FS=OFS=","} {print $2, $1}' inputfile.csv > col2-1.csv
cat col2-1.csv >> inputfile.csv

Answered By – MarJoh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published