Format a date on additional column awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Format a date on additional column awk
# 1  
Old 03-05-2019
Format a date on additional column awk

Hi,
My objective is to achieve from HB.txt
Code:
05/20/1997,1130,5.93,5.96,5.93,5.96,49200
05/20/1997,1131,5.96,5.96,5.9,5.93,252400
05/14/1997,1132,5.93,5.99,5.93,5.99,89600
05/15/1997,1133,5.93,5.93,5.71,5.74,203200

into adding a day column by using the first column - date.
Code:
05/14/1997,1132,5.93,5.99,5.93,5.99,89600,Wed
05/15/1997,1133,5.93,5.93,5.71,5.74,203200,Thu
05/20/1997,1130,5.93,5.96,5.93,5.96,49200,Tue
05/20/1997,1131,5.96,5.96,5.9,5.93,252400,Tue

So far, I'm in progress to try this,
Code:
awk  'BEGIN{RS="\r"; FS=",";}{printf $0 "," $1;}' HB.txt > newHB.csv

However, the generated output for the new column is after a breakline.
Code:
05/14/1997,1132,5.93,5.99,5.93,5.99,89600,05/14/1997
05/15/1997,1133,5.93,5.93,5.71,5.74,203200
05/20/1997,1130,5.93,5.96,5.93,5.96,49200,
05/15/1997
05/20/1997,1131,5.96,5.96,5.9,5.93,252400,
05/20/1997

System Specifications:
macOS High Sierra
Terminal - Bash

Last edited by benchin_; 03-06-2019 at 03:24 AM..
# 2  
Old 03-05-2019
Not the "day of the week" column, but...
Code:
awk -F, '{print $0, $1}' OFS= myFile

As a hint for the "day of the week":
Code:
$ date -d 05/14/1997
Wed, May 14, 1997 12:00:00 AM


Last edited by vgersh99; 03-05-2019 at 01:09 PM..
# 3  
Old 03-05-2019
Welcome to the forum.


Please post your OS, shell, and preferred tools' versions in the future so people in here don't need to guess what system features you have available and which you don't.



Your RS character \r (<CR> = 0x0D = ^M) seems a bit strange a choice as \n (<LF> = 0x0A) is the *nix line terminator. How was that input file produced? And, no attempt was made to get to the desired day-of-week result.


GNU date around, and a shell with "process substitution"? Try
Code:
paste -d, file <(date -f<(awk -F"," '{print $1}' file) +%a)
05/20/1997,1130,5.93,5.96,5.93,5.96,49200,Di
05/20/1997,1131,5.96,5.96,5.9,5.93,252400,Di
05/14/1997,1132,5.93,5.99,5.93,5.99,89600,Mi
05/15/1997,1133,5.93,5.93,5.71,5.74,203200,Do

This User Gave Thanks to RudiC For This Post:
# 4  
Old 03-05-2019
If your awk supports, try if this works for you :
Code:
awk -F"," ' { split($1,a,"/") ; t=mktime(a[3] " " a[1] " " a[2] " 00 00 00") ; print $0,strftime("%a",t) } ' HB.txt

Hope that helps
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 5  
Old 03-05-2019
Updated*
System Specifications:
macOS High Sierra
Terminal - Bash

Thanks for the help/clues so far, I'll proceed to try it again
# 6  
Old 03-05-2019
The date utility, the awk utility, and the bash utility on macOS don't behave as the current GNU utility extensions that are referenced elsewhere in this thread, but if you're willing to use ksh instead of bash, you could try:
Code:
#!/bin/ksh
while IFS=, read -r date rest
do	printf '%s,%s,%(%a)T\n' "$date" "$rest" "$date"
done < HB.txt

which, with your sample input file, produces the output:
Code:
05/20/1997,1130,5.93,5.96,5.93,5.96,49200,Tue
05/20/1997,1131,5.96,5.96,5.9,5.93,252400,Tue
05/14/1997,1132,5.93,5.99,5.93,5.99,89600,Wed
05/15/1997,1133,5.93,5.93,5.71,5.74,203200,Thu

which seems to be what you want except that this will produce 3 character abbreviations for all days of the week instead of producing "Tues" for Tuesdays as you seem to want. Is this close enough?

This was tested on macOS Mojave version 10.14.3 instead of on macOS High Sierra, but I don't think ksh has changed significantly between these two releases.
# 7  
Old 03-06-2019
Yes this is close enough. Thanks.

However, I am getting this instead (While using KSH).

Code:
,Wed4/1997,1132,5.93,5.99,5.93,5.99,89600
05/15/1997,1133,5.93,5.93,5.71,5.74,203200,Thu
,Tue0/1997,1130,5.93,5.96,5.93,5.96,49200

Could it be my system is not optimized for this?
I having issues with the break lines.

Last edited by benchin_; 03-06-2019 at 02:43 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

2. Shell Programming and Scripting

How to change the format of the date column in a flat file?

Hi, i have a flat file namely temp.txt with this data below ID|name|contact_date 101|Kay|2013-12-26 102|let|2013-12-26 I need to modify the date data in the flat file into MM/DD/YYYY HH24:MI:SS format let me know the code for this. Thank you! (5 Replies)
Discussion started by: srikanth_sagi
5 Replies

3. Shell Programming and Scripting

Convert date column as yyyy/mm/dd format

Hi All, I have file like “April 10, 2013”,”raj” “April 29, 2013”,”raj1” Output : “2013/04/10”,”raj” “2013/04/29”,”raj1” Please help me how to do... (9 Replies)
Discussion started by: bmk
9 Replies

4. Shell Programming and Scripting

awk convert date format

Could you tell me how to convert the following dates? If I have m/d/yyyy, I want to have 0m/0d/yyyy. I want my dates to always be 8 digits. In other words, I want a 0 inserted whenever the month or day is a single digit. My issue is first I need to use FS="," to get field $4 for the... (7 Replies)
Discussion started by: wbrunc
7 Replies

5. Shell Programming and Scripting

awk - change date format

I have below date format in a CSV file. (dd/mm/yyyy) Ex Input: 9/8/2013 Need to convert it into below format (yyyymmdd ) and redirect to new file. Ex Output: 20130809 How do I use awk here to change the format and if leading 0 (zero) is not then add it. Please help. Thanks. (8 Replies)
Discussion started by: vegasluxor
8 Replies

6. Shell Programming and Scripting

awk compare column with date format

I have this code to compare columns 1 and 10 between file1 and file 2 and give me all records that match column 1 but dont match column 10 However column 10 is date format mm/dd/yy and awk cant read it and compare ...i tried awk < file1 -F~ '{print $10}' and it gave blank screen Is... (1 Reply)
Discussion started by: sigh2010
1 Replies

7. Shell Programming and Scripting

Convert the date format from mdy to ymd in column of file

The date format in the delimited file for one column '6/27/2011 12:00:00 AM' Is it possible o change it to '2011-06-27 12:00:00 AM' for all the records.. Thanks in advance..... (8 Replies)
Discussion started by: infernalhell
8 Replies

8. UNIX for Dummies Questions & Answers

Format Date in awk

I have an assignment in a Linux class I am taking. It has multiple scripts. Basicly when it runs it asks the user name and shows information about the user from the /etc/passwd and /etc/shadow files. The one I need help with is the one that reads the /etc/shadow file. I need to format the date into... (1 Reply)
Discussion started by: kazulk
1 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question