How to add a prefix to every row of a particular column in a csv file via command line on linux

1

So basically i am trying to achieve the following. 

 

File before editing. 

column-1,   column-2,  column-3,  column-4,  column-5
Row-1-c1,  Row-1-c2,  Row-1-c3,  Row-1-c4,  Row-1-c5
Row-2-c1,  Row-2-c2,  Row-2-c3,  Row-2-c4,  Row-2-c5
Row-3-c1,  Row-3-c2,  Row-3-c3,  Row-3-c4,  Row-3-c5
Row-4-c1,  Row-4-c2,  Row-4-c3,  Row-4-c4,  Row-4-c5
Row-5-c1,  Row-5-c2,  Row-5-c3,  Row-5-c4,  Row-5-c5
 
File after editing 

column-1,   column-2,    column-3,               column-4,    column-5
Row-1-c1,   Row-1-c2,   Prefix-Row-1-c3,    Row-1-c4,   Row-1-c5
Row-2-c1,   Row-2-c2,   Prefix-Row-2-c3,    Row-2-c4,   Row-2-c5
Row-3-c1,   Row-3-c2,   Prefix-Row-3-c3,    Row-3-c4,   Row-3-c5
Row-4-c1,   Row-4-c2,   Prefix-Row-4-c3,    Row-4-c4,   Row-4-c5
Row-5-c1,   Row-5-c2,   Prefix-Row-5-c3,    Row-5-c4,   Row-5-c5

notice that column-3 is the column that the prefix is added to each individual row except the column heading. 

Jerome Ricketts

Posted 2014-06-15T01:59:46.023

Reputation: 19

Answers

1

Replace $3 with the column-to-change's number, <prefix> with the prefix, <inputfile> and <outputfile> with the input filename and the output filename respectively.

awk '{$3="<prefix>"$3; print}' <inputfile> ><outputfile>

I noticed in your example that the first line was not modified; if you want this behavior, use the following command instead:

awk '{if(NR==1){print; next}; $3="<prefix>"$3; print}' <inputfile> ><outputfile>

BenjiWiebe

Posted 2014-06-15T01:59:46.023

Reputation: 7 672