0
  • Every line is separated into parts with a semicolon

  • In every part, there are dates in different formats

  • Lines can include digits, letters and spaces.

I am trying to change the format of the date in the 6th part. It is currently like this

12 Nov 1999 

but I only need the year. So how can I omit the day and month in the 6th parts of the lines without touching the other parts

12/31/1921;12 nov 1998; 13 September 2019; 13 Sep 2018; 12 August 1998; 12 Nov 1999; 27 November 1999; 13 Dec 1999; 14 December 1999; 15 D 1999

would turn into this

12/31/1921;12 nov 1998; 13 September 2019; 13 Sep 2018; 12 August 1998;1999; 27 November 1999; 13 Dec 1999; 14 December 1999; 15 D 1999
1
awk -F ';' 'BEGIN { OFS=FS } { sub(".* ", "", $6); print }' file

The above parses each line of file as a set of ;-delimited fields. It applies a substitution that modifies the 6th field of each line. The modification involves removing everything up to the last space character in the field's data. After modifying the 6th field, the modified line is printed.

1

With awk's substr function:

awk -F';' '{ $6 = substr($6, length($6)-3) }1' OFS=';' inputfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.