1

I have many csv file in which I need to manipulate the first field which contains the date and then print the rest of the line. The files have varying field lengths.

My sample line is

"11.07.2016 00:00:00",DON1SOE02,PAPN,PAPN,OPEN1000,918945,

I have tried the code below

awk -F"," '{print "\""substr($1,08,4)"-"substr($1,5,2)"-"substr($1,2,2)substr(‌​$1,12,9)"\","$0""}' file.csv    

The result of this is

"2016-07-11 00:00:00","11.07.2016 00:00:00",DON1SOE02,PAPN,PAPN,OPEN1000,918945,

Is there any way to avoid printing the unmodified date field, i.e. "11.07.2016 00:00:00"

solution below has a way to skip the first column, but I would like to modify the first column then print the modification and then skip printing unmodified first column

Using awk to print all columns from the nth to the last

Community
  • 1
  • 1
Santosh Pillai
  • 1,069
  • 17
  • 29

1 Answers1

3

You must give the new value to your the first field: awk '{$1="..."}1. Otherwise the whole line $0 will stay unchanged:

awk -F, -v OFS=, '{$1="\""substr($1,8,4)"-"substr($1,5,2)"-"substr($1,2,2)""substr($1,12,9)"\""}1' file.csv
fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113