关于 UNIX Grep 命令
我需要编写一个 shell 脚本来选取 /exp/files 目录中的所有文件(而不是目录)。对于目录内的每个文件,我想查找是否收到文件的最后一行。文件中的最后一行是预告片记录。最后一行中的第三个字段也是数据记录数,即2315(文件中的总行数-2(标题,预告片))。在我的unix shell脚本中,我想通过检查T来检查最后一行是否是预告片记录,并想检查文件中的行数是否等于(2315+2)。如果成功,那么我想将文件移动到另一个目录/exp/ready。
tail -1 test.csv
T,Test.csv,2315,80045.96
另外,在输入文件中,有时预告片记录的 0 或 1 个以上字段可以用双引号引起来
"T","Test.csv","2315","80045.96"
"T", Test.csv, 2212,"80045.96"
T,Test.csv,2315,80045.96
I need to write a shell script that pick all the files (not directories) in /exp/files directory. For each file inside the directory I want to find whether the last line of file is received . The last line in the file is a trailer record. Also the third field in the last line is the number of data records count i.e 2315 (Total Number of lines in the file -2 (header,trailer) ) . In my unix shell script i want to check whether the last line is a trailer record by checking T and want to check whether the number of lines in the file is equal to (2315+2). If this is successful then i want to move the file to a different directory /exp/ready.
tail -1 test.csv
T,Test.csv,2315,80045.96
Also in the inputfile sometimes 0 or 1 more fields of trailer record can be within double quotes
"T","Test.csv","2315","80045.96"
"T", Test.csv, 2212,"80045.96"
T,Test.csv,2315,80045.96
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用以下命令测试最后一行是否存在:
此时,如果该行以
T,
或"T 开头,则
,假设这足以捕捉预告片记录。$rc
将为 0 ”,一旦确定了这一点,您就可以使用以下命令提取行数:
并且可以使用以下命令获取预期行数
并比较两者。
因此,将所有这些结合在一起,这将是一个良好的开始。它输出文件本身(我的测试文件
num[1-9].tst
)以及一条消息,指示文件是否正常或为什么不正常。示例运行显示了我使用的测试文件:
You can test for the presence of the last line with the following:
At that point
$rc
will be 0 if the line started with eitherT,
or"T",
, assuming that's enough to catch the trailer record.Once you've established that, you can extract the line count with:
and you can get the expected line count with:
and compare the two.
So, tying all that together, this would be a good start. It outputs the file itself (my test files
num[1-9].tst
) along with a message indicating whether the file is okay or why it is not okay.The sample run, showing the test files I used:
如果您想在写入并关闭文件后移动文件,那么您应该考虑使用 inotify、incron、FAM、gamin 等。
If you want to move the files after they've been written and closed then you should consider using something like inotify, incron, FAM, gamin, etc.
该代码通过一次 awk 调用完成所有逻辑计算,这使得它非常高效。它还不对示例值 2315 进行硬编码,而是使用预告片行中包含的值,因为我相信这是您的意图。
如果您对结果满意,请记住删除
echo
。更新
我必须添加
{v0=$0;v1=$1;v3=$3}
因为 SunOS 的 awk 实现不支持 END{} 访问字段变量($0、$1、$2 等) .),但如果您想在 END{} 内处理它们,则必须将其保存到用户定义的变量中。请参阅此 awk 功能比较链接中第一个表的最后一行This code does all of the logic calculations via a single call to awk which makes it very efficient. It also does NOT hardcode the example value of 2315 but rather uses the value contained in the trailer line as I believe this was your intent.
Remember to remove the
echo
if you are satisfied with the results.Update
I had to add
{v0=$0;v1=$1;v3=$3}
because SunOS's implementation of awk does not support END{} having access to the field variables ($0, $1, $2, etc.) but instead must be saved to a user-defined variable if you want to work on them inside END{}. See the last row of the first table in This awk feature comparison link这里没有方便的 UNIX shell,但
应该将所有文件放在 BASH 数组中;然后按照上面建议的 paxdiablo 迭代它们中的每一个应该可以让你排序
Don't have a UNIX shell handy here, but
should put all files in a BASH array; then iterating through each of them as paxdiablo suggested above should get you sorted