Reputation: 117
I'm sure this has been answered but having trouble with my google search.
File is like this:
2020-01-01 10:33
2020-01-01 14:04
2020-01-01 17:22
2020-01-20 14:04
2020-01-21 03:33
2020-01-22 14:06
How do I traverse through each line and apply date +"%b%d" so that the file looks like:
Jan1 10:33
Jan1 14:04
Jan1 17:22
Jan20 14:04
Jan21 03:33
Jan22 14:06
Upvotes: 1
Views: 70
Reputation: 84579
You have basically two options.
+'%b%d %H:%M'
; orawk
, create a datespec from each line, and pass to mktime
to create a timestamp which can then be used with strftime
to output the format "%b%d %H:%M"
.Bash Solution
while read -r line; do
date -d "$line" +'%b%d %H:%M'
done < file
GNU awk Solution
awk '{
gsub(/[-:]/," ",$0) # remove '-' and ':'
ts=mktime($0 " 00") # add " 00" creating datespec, create timestamp
print strftime("%b%d %H:%M",ts) # output new date format
}' file
(note: mawk
, at least from version 1.3.3 on also supports mktime
and strftime
)
Example Output
In both cases the output is the same:
Jan01 10:33
Jan01 14:04
Jan01 17:22
Jan20 14:04
Jan21 03:33
Jan22 14:06
Look things over and let me know if you have additional questions.
Upvotes: 3