awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)
# 1  
Old 05-10-2019
awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy and replace it with yyyy.

my csv looks like
Code:
"abc","sdf","sdf","we","qe","fr","ty","hgf","er","jy","PERIOD_END_DATE"
"123","","234","ety","","ghj","tyu","Inh","dn","ngnh","17-FEB-19"

what i want to do is use an awk script to print the 11th column twice and extract the year from the data and print it in yyyy format as below:


Code:
"PERIOD_END_DATE" "YEAR"
"17-FEB-19" "2019"

Currently i can extract the following from my csv:

Code:
"PERIOD_END_DATE" "PERIOD_END_DATE"
"17-FEB-19" "17-FEB-19"

using the awk script as follows:
Code:
awk -F ',' '{print $11,$11"\r"}' test1.csv > test2.csv

But what i need is the second column header to be replaced to "Year" and extract the year from the columns and print it in yyyy format for the whole column, so it will look like :
Code:
 "PERIOD_END_DATE" "YEAR"
"17-FEB-19" "2019"

Can anyone help me with this, Please ask for more clarity if this is unclear.

Last edited by Kunalcurious; 05-10-2019 at 08:52 AM.. Reason: Added more clarity
# 2  
Old 05-10-2019
Code:
$ awk -F, -v yr='"YEAR"' ' { $0=$0 OFS (NR == 1 ? yr : "20" substr($0,length($0)-2,3)) } 1 ' OFS=","  file
"abc","sdf","sdf","we","qe","fr","ty","hgf","er","jy","END_DATE","YEAR"
"123","","234","ety","","ghj","tyu","Inh","dn","ngnh","17-FEB-19",2019"

# 3  
Old 05-10-2019
will this year replacement will replace any year, say the data might have 2018, 2019, 2015, 2016, so all it gotta do is extract 18, 19 and add 20 in front so the value becomes 2014,2015,2016,2017,2018, 2019. Irrespective of the year. is this possible? Also, the file is a csv so I want to pass the csv file to the awk script since the file is huge, as can be seen i had input csv file to the script
test1.csv > test2.csv
# 4  
Old 05-10-2019
Yes this will replace any year(If year is less than 2000 then prefix 19) and you can pass csv file to awk
# 5  
Old 05-10-2019
@anbu23 for some reason it acts funny. the command I used is:

Code:
awk -F, -v yr='"YEAR"' ' { $0=$0 OFS (NR == 1 ? yr : "20" substr($0,length($0)-2,3)) } 1 ' OFS="," testk3.csv > test5.csv

and the output is:
Code:
,"YEAR"_END_DATE" "PERIOD_END_DATE"
,209"EB-19" "17-FEB-19"
,209"EB-19" "24-FEB-19"
 ,209"EB-19" "24-FEB-19"

Note: I made 1 mistake on my part while asking the question and before making an edit i mentioned "END_DATE" instead of "PERIOD_END_DATE". I apologize for the inconvenience.

Last edited by Kunalcurious; 05-10-2019 at 09:16 AM.. Reason: mentioned the advisor
# 6  
Old 05-10-2019
can you post sample data from testk3.csv?
# 7  
Old 05-10-2019
These are some lines from the file:


"PERIOD_END_DATE" "PERIOD_END_DATE"
"17-FEB-19" "17-FEB-19"
"24-FEB-19" "24-FEB-19"
"24-FEB-19" "24-FEB-19"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies

2. UNIX for Beginners Questions & Answers

Extract a column and multiple by 1000 and replace it on same file

Hi All, I need to extract a position in the file and multiple the value by 1000 and the replace it . Original 0010001200084701217637306521200000000000010010000000 ---> 000847 * 1000 0010012700086001213437404323000000000000001001000000 ---> 000860 * 1000... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

3. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

4. Shell Programming and Scripting

Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc,... (8 Replies)
Discussion started by: Eric1
8 Replies

5. Shell Programming and Scripting

Replace a column with a value by ignoring the header lines

i have a file in the gz format , the content of the file is as follow. gzcat f1.gz # 1.name # 2.location # 3.age # 4.dob . . . . . . . . . # 43.hobbies < Aravind,33,chennai,09091980, , , , , , , surfing> (5 Replies)
Discussion started by: aravindj80
5 Replies

6. Shell Programming and Scripting

How to Replace the value of a column using awk command?

Hi cat test.txt H|123|341|567|asfg D|dfg|trtyy|errt D|ert|frty|wer Here I need to replace the third column value with 100 of the first record only and while printing I need to print the full file content also..I am expecting a result like this H|123|100|567|asfg D|dfg|trtyy|errt... (3 Replies)
Discussion started by: saj
3 Replies

7. Shell Programming and Scripting

Replace the nth column date as MM/DD/YYYY

Hi, I need some unix command to replace the following thing. cat test.dat 1234|test|8/19/2009|8/20/2009|test 1234|test|8/9/2009|8/21/2009|test 1234|test|8/1/2009|8/2/2009|test after processing 1234|test|08/19/2009|08/20/2009|test 1234|test|08/09/2009|08/21/2009|test... (6 Replies)
Discussion started by: anshaa
6 Replies

8. Shell Programming and Scripting

awk to replace part of a column

dear all, I'm trying to use Awk to eliminate the last two characters from the first column in a file. This two characters are "-1" and I need to eliminate them from each row that I have in the files. The files have two columns and look like: ID_090-1 2 ID_3787-1 4 ID_0098-1 1 ID_12-1 4 I... (4 Replies)
Discussion started by: gabrysfe
4 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. Shell Programming and Scripting

changing YYYY-MM-DD to DDMMYY

Hi all Wonder if you can help , I have a comma delimited file where the 13th to the 31st character of each line is populated by the date and time."2004-07-21 09:39:06" 16-5-141551,2004-07-21 09:39:06,8815 22-5-340394,2004-07-22 09:39:41,8001 22-5-340433,2004-07-23 09:42:20,1701 I have... (3 Replies)
Discussion started by: hcclnoodles
3 Replies
Login or Register to Ask a Question