Shantanu Bedajna
Shantanu Bedajna

Reputation: 581

Shell script awk concatenate text with entire column

I have a csv file Final.csv and i am trying to change its first coulmn and add a text it each column row like this is the csv

Trialtime   type    Track   time    Notes   Athlete
07:15:00    Warmup  ABC 85.2    Initial warmup  Jon
07:45:00    Sprint1 ABC 59.44   First Sprint    Jon
08:30:00    TRIAL   ABC 57.21   Final attempt   Jon
08:00:00    Warmup  ABC 120.51  Initial warmup  Bill
08:40:05    Sprint1 ABC 61.35   First Sprint    Bill
09:15:00    Sprint2 ABC 60.08   Second Sprint   Bill
10:30:00    TRIAL   ABC 60.37   Final attempt   Bill

and i want to add a text to the trial time coulmn like this

Trialtime   type    Track   time    Notes   Athlete
2012 07:15:00   Warmup  ABC 85.2    Initial warmup  Jon
2012 07:45:00   Sprint1 ABC 59.44   First Sprint    Jon
2012 08:30:00   TRIAL   ABC 57.21   Final attempt   Jon
2012 08:00:00   Warmup  ABC 120.51  Initial warmup  Bill
2012 08:40:05   Sprint1 ABC 61.35   First Sprint    Bill
2012 09:15:00   Sprint2 ABC 60.08   Second Sprint   Bill
2012 10:30:00   TRIAL   ABC 60.37   Final attempt   Bill

i try to print each line row in that coulmn like this with awk

awk -F',' 'NR>1{print $1}1' OFS=',' $OutFileName

NR>1 is for not including the headers but i want to add a text variable to each row in the coulmn

lets say

text="2012"

now i want to modify the trial time column rows with this 2012 appended to each row what i wa doing was

awk -F',' 'NR>1{$1$text;}1' OFS=',' $OutFileName

but the text variable does not work inside awk what to do ? and i also want to keep the headers.

Upvotes: 1

Views: 229

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk solution:

awk -v t="2012" 'BEGIN{ FS=OFS="\t" }NR>1{ $1=t" "$1 }1' file

The output:

Trialtime   type    Track   time    Notes   Athlete
2012 07:15:00    Warmup  ABC 85.2    Initial warmup  Jon
2012 07:45:00    Sprint1 ABC 59.44   First Sprint    Jon
2012 08:30:00    TRIAL   ABC 57.21   Final attempt   Jon
2012 08:00:00    Warmup  ABC 120.51  Initial warmup  Bill
2012 08:40:05    Sprint1 ABC 61.35   First Sprint    Bill
2012 09:15:00    Sprint2 ABC 60.08   Second Sprint   Bill
2012 10:30:00    TRIAL   ABC 60.37   Final attempt   Bill

Upvotes: 1

Kent
Kent

Reputation: 195039

awk -v txt="2012" 'NR==1||$0=txt FS $0' yourFile

For the input and output example, this will do what you want.

Upvotes: 2

Related Questions