如何以所需的格式显示输入

发布于 2024-12-01 13:56:44 字数 626 浏览 1 评论 0 原文

输入文件包含一些内容,例如:

15-05-2011  16:05    <DIR>          .
15-05-2011  16:05    <DIR>          ..
24-04-2011  16:07    <DIR>          Administrator
15-05-2011  16:05    <DIR>          confuser
01-02-2011  20:57    <DIR>          Public
29-01-2011  19:28    <DIR>          TechM
12-08-2011  09:36    <DIR>          vt0013487

我需要在命令行参数中给出文件名,

输出采用所需的格式:

Administrator 24-04-2011  16:07 
confuser      15-05-2011  16:05 
Public        01-02-2011  20:57 
TechM         29-01-2011  19:28 
vt0013487     12-08-2011  09:36

input file contains some contents such as :

15-05-2011  16:05    <DIR>          .
15-05-2011  16:05    <DIR>          ..
24-04-2011  16:07    <DIR>          Administrator
15-05-2011  16:05    <DIR>          confuser
01-02-2011  20:57    <DIR>          Public
29-01-2011  19:28    <DIR>          TechM
12-08-2011  09:36    <DIR>          vt0013487

I need to give the file name in the command line argument

the output to be in the desired format:

Administrator 24-04-2011  16:07 
confuser      15-05-2011  16:05 
Public        01-02-2011  20:57 
TechM         29-01-2011  19:28 
vt0013487     12-08-2011  09:36

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2024-12-08 13:56:44

因此,您正在执行一些固定宽度字段输入解析,但最终字段是可变长度并延伸到行尾。这很容易。唯一尴尬的一点是我们需要读入所有行才能获取输出格式第一个字段的宽度。

假设您在 stdin 上提供输入(即通过重定向)并希望在 stdout 上提供输入(因此您也可以将其重定向到文件):

##### Read in and compute the width
set len 0
while {[gets stdin line] >= 0} {
    set date [string range $line 0 16]
    set name [string range $line 36 end]
    lappend lines $name $date
    if {[string length $name] > $len} {
        set len [string length $name]
    }
}
##### Write out as formatted
foreach {name date} $lines {
    puts [format "%-*s %s" $len $name $date]
}

So you're doing some fixed-width-field input parsing except that the final field is variable length and extends to the end of the line. That's easy enough. The only awkward bit is that we need to read in all the lines to get the width for the first field of the output format.

Assuming you supply the input on stdin (i.e., by redirection) and want it on stdout (so you can also redirect that to a file):

##### Read in and compute the width
set len 0
while {[gets stdin line] >= 0} {
    set date [string range $line 0 16]
    set name [string range $line 36 end]
    lappend lines $name $date
    if {[string length $name] > $len} {
        set len [string length $name]
    }
}
##### Write out as formatted
foreach {name date} $lines {
    puts [format "%-*s %s" $len $name $date]
}
只是我以为 2024-12-08 13:56:44

split 将输入分成行,foreach 迭代它们,regexp 从这些行中提取相关的字符组,format 构造结果字符串(Tcl 中通常不需要 format,因为字符串中的简单变量替换通常适用于常见情况)。

阅读这个, 这个。还有这个用于regexp<使用的语法/code> 匹配引擎。

另外,我怀疑您可能正在尝试使用 DOS dir 命令的 exec 生成的输出,而不是使用 glob 来遍历目录和文件原生地。如果是这样,这是错误的,请使用 glob< /a>

split to break the input into lines, foreach to iterate over them, regexp to extract relevant groups of characters from those lines, format to construct resulting strings (format is often not needed in Tcl as simple variable substitution in strings usually works just okay for common cases).

Read this, this, this and this. Also this for the syntax used by the regexp matching engine.

Also I suspect you may be trying to use the output generated by exec'ing the DOS dir command instead of using glob to traverse directories and files natively. If so, this is wrong, use glob

呆橘 2024-12-08 13:56:44

这可能无法回答您的问题:如果您正在调用 cmd /c dir,这里有一种在 Tcl 中执行此操作的方法:

package require struct::list
set files [glob *]
set maxlen [tcl::mathfunc::max {*}[struct::list map $files {apply {s {string length $s}}}]]
foreach file [lsort $files] {
    set datetime [clock format [file mtime $file] -format {%d-%m-%Y %H:%M}]
    puts [format {%-*s %s} $maxlen $file $datetime]
}

This may not answer your question: If you are calling cmd /c dir, here's a way to do it in Tcl:

package require struct::list
set files [glob *]
set maxlen [tcl::mathfunc::max {*}[struct::list map $files {apply {s {string length $s}}}]]
foreach file [lsort $files] {
    set datetime [clock format [file mtime $file] -format {%d-%m-%Y %H:%M}]
    puts [format {%-*s %s} $maxlen $file $datetime]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文