unix shell脚本用T + 2更新文件中出现的日期(T)

时间:2018-01-10 09:36:12

标签: shell unix

有一个file.txt,内容如下所述

user,joinningDate
A,14/Dec/17
B,29/Dec/17
C,30/Nov/17
D,29/Sep/17
E,28/Feb/17

我需要一个unix shell脚本,可以使用JoiningDate + 2更新JoiningDate,也应该忽略周末。所以输出应该如下所示

user,joinningDate
A,18/Dec/17
B,02/Jan/18
C,04/Dec/17
D,03/Oct/17
E,02/Mar/17

1 个答案:

答案 0 :(得分:0)

使用Shell脚本与 -

共享代码
#!/bin/sh

# @Author Jatish.Khanna[@]gmail[.]com

if [ $# -eq 0 ]; then

 echo "Exiting - input file not specified"
 exit
fi

count=0
while IFS='' read -r line || [[ -n "$line" ]]; do

#Skipping header from the input file - user,joinningDate
if [ $count -eq 0 ]; then
   count=$((count + 1))
   continue
 fi

# getting the date
found_date_old=$(echo $line | cut -d, -f2)

# converting into required format
found_date=$(echo  $found_date_old | tr '/' ' ')

# addind number of days to the date
new_date=$(date +%d/%b/%y -d "$found_date 2 day")

record=$(echo $line | sed "s|${found_date_old}|${new_date}|")
echo "$record"

done < "$1"

对此解决方案的假设 - a)输入格式预期为 -

[任何文字],日期[,任何文字]

使用&#34;,&#34;分隔输入日期。并预计将在第二栏

注意:可以通过更新部分 -

轻松自定义

获取日期

found_date_old=$(echo $line | cut -d, -f2)

b)Linux $(日期)命令

应接受列中的输入日期

c)预期的输出格式可以自定义,而我们有大量的选项可用日期命令