文件操作:脚本问题 [英] File Manipulation: Scripting Question

查看:38
本文介绍了文件操作:脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本连接到数据库并获取所有可查询查询的记录.这些记录结果是服务器上存在的文件,所以现在我有了一个文本文件,其中包含所有文件名.

I have a script which connects to database and gets all records which statisfy the query. These record results are files present on a server, so now I have a text file which has all file names in it.

我想要一个脚本,该脚本会知道:

I want a script which would know:

  1. output.txt文件中每个文件的大小是多少?
  2. 该文本文件中所有文件的总大小是多少?
  1. What is the size of each file in the output.txt file?
  2. What is the total size of all the files present in that text file?

更新: 我想知道如何使用Perl programming language完成任务,我们将不胜感激.

Update: I would like to know how can I achieve my task using Perl programming language, any inputs would be highly appreciated.

注意:我没有任何特定的语言限制,可以是Perl或Python脚本语言,可以从Unix提示符下运行.目前,我正在使用bash shell,并且具有shpy脚本.该怎么办?

Note: I do not have any specific language constraint, it could be either Perl or Python scripting language which I can run from the Unix prompt. Currently I am using the bash shell and have sh and py script. How can this be done?

我的脚本:

#!/usr/bin/ksh
export ORACLE_HOME=database specific details
export PATH=$ORACLE_HOME/bin:path information
sqlplus database server information<<EOF
SET HEADING OFF
SET ECHO OFF
SET PAGESIZE 0
SET LINESIZE 1000
SPOOL output.txt
select * from my table_name;
SPOOL OFF
EOF

我知道du -h是我应该使用的命令,但是我不确定我的脚本应该是什么,我已经在python中尝试了一些东西.我对Python完全陌生,这是我的第一次努力.

I know du -h would be the command which I should be using but I am not sure how should my script be, I have tried something in python. I am totally new to Python and it's my first time effort.

这里是:

import os

folderpath='folder_path'
file=open('output file which has all listing of query result','r')

for line in file:
 filename=line.strip()
 filename=filename.replace(' ', '\ ')
 fullpath=folderpath+filename
# print (fullpath)
 os.system('du -h '+fullpath)

例如,输出文本文件中的文件名如下:007_009_Bond Is Here_009_Yippie.doc

File names in the output text file for example are like: 007_009_Bond Is Here_009_Yippie.doc

任何指导将不胜感激.

更新:

  1. 如何使用Perloutput.txt文件中存在的所有文件移动到其他文件夹位置?
  2. 执行步骤1之后,如何删除output.txt文件中存在的所有文件?
  1. How can I move all the files which are present in output.txt file to some other folder location using Perl ?
  2. After doing step1, how can I delete all the files which are present in output.txt file ?

任何建议将不胜感激.

Any suggestions would be highly appreciated.

推荐答案

在perl中,-s filetest运算符可以满足您的需求.

In perl, the -s filetest operator is probaby what you want.

use strict;
use warnings;
use File::Copy;

my $folderpath = 'the_path';
my $destination = 'path/to/destination/directory';
open my $IN, '<', 'path/to/infile';
my $total;
while (<$IN>) {
    chomp;
    my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
    move("$folderpath/$_", "$destination/$_") or die "Error when moving: $!";
}
print "Total => $total\n";

请注意,-s的大小以字节为单位,而不是,如du.

Note that -s gives size in bytes not blocks like du.

在进一步研究中,perl的-s等同于du -b.您可能应该阅读特定du上的手册页,以确保您实际上正在测量要测量的内容.

On further investigation, perl's -s is equivalent to du -b. You should probably read the man pages on your specific du to make sure that you are actually measuring what you intend to measure.

如果您确实需要du值,请将上面的$size的赋值更改为:

If you really want the du values, change the assignment to $size above to:

my ($size) = split(' ', `du "$folderpath/$_"`);

这篇关于文件操作:脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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