使用ksh从行中解析名称和日期,如果日期早于50天,则删除该行 [英] Use ksh to parse a name and a date from a line and remove the line if the date is older than 50 days

查看:88
本文介绍了使用ksh从行中解析名称和日期,如果日期早于50天,则删除该行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下格式的测试文件,其中包含用户从数据库锁定时的用户名日期.

I have a test file with the following format, It contains the username_date when the user was locked from the database.

$ cat lockedusers.txt
TEST1_21062016          
TEST2_02122015  
TEST3_01032016  
TEST4_01042016
$

我正在编写一个ksh脚本,在我的脚本级别上遇到了这种困难的情况.我想做的是:

I'm writing a ksh script and faced with this difficult scenario for my level of scripting. What I would like to do is:

  1. 从文本文件中读取行,
  2. 如果此行的日期值早于50天,
  3. 然后读取该行,直到下划线字符之前,例如TEST1放入变量
  4. 然后删除此行.

该变量将用于从数据库中删除用户.任何帮助将不胜感激.

The variable will be used for removing the user from database. Any help would be appreciated.

推荐答案

非常感谢您的帮助-这是此问题的解决方案,其中不包括如何从数据库购买中删除用户,因此很容易为任何人找出该代码将生成超过50天的用户列表.

veryone, thanks for your help - here's the solution for this problem not including how to remove users from teh database buyt it should be easy to figure out for anyone as this code will generate the user list that are older than 50 days.

#!/bin/ksh
echo
cat test |
while IFS="_ " read LUSER LDATE
do
        Day=`echo $LDATE | cut -c1,2`
        Mon=`echo $LDATE | cut -c3,4`
        Year=`echo $LDATE | cut -c5-8`
        Date=`echo $Mon/$Day/$Year`
        if [[ $(( $(date +%s) - $(date +%s -d${Date}) )) -ge 4320000 ]] ; then
        #echo "$LUSER, $LDATE"i
        echo "$LUSER" >> userRemovalList
        grep -v "$LUSER" test > temp && mv temp test
        fi
done
chmod 755 userRemovalList

从我的同事Derek那里获得UNIX帮助程序门户网站的另一个帮助程序的信用

Credit to my colleague Derek another helpers from UNIX helper portal http://www.unix.com/shell-programming-and-scripting/267002-ksh-read-line-parse-characters-into-variable-remove-line-if-date-older-than-50-days-2.html#post302977045

这篇关于使用ksh从行中解析名称和日期,如果日期早于50天,则删除该行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆