有没有一种方法可以在一段时间内使用较少的命令?

发布于 2025-01-23 15:34:15 字数 630 浏览 1 评论 0原文

我有一个循环进行输入并为每个条目创建一个printf。这个文件真的很大,因此打印出来将永远需要。有什么方法可以将结果保存到文件中,并使用使其成为打页的输出?

while read line
do
            id=$(cut -d' ' -f1 <<< $line)
            ip=$(cut -d' ' -f2 <<< $line)
            mon=$(cut -d' ' -f3 <<< $line)
            day=$(cut -d' ' -f4 <<< $line)

            printf "%s 45%s (%s %s)\n" "$id" "$ip" "$mon" "$day"
done <<< "$inputFile"

我尝试了此操作

result+=$("%s 45%s (%s %s)\n" "$id" "$ip" "$mon" "$day")

,然后只是

less "$result"

在循环外进行,但这不起作用,我不知道该从这里去哪里。

I have this while loop that takes an input and creates a printf for each entry. This file is really big so having it print out will take forever. Is there a way I can save the result to a file and use less to have it be a paginated output?

while read line
do
            id=$(cut -d' ' -f1 <<< $line)
            ip=$(cut -d' ' -f2 <<< $line)
            mon=$(cut -d' ' -f3 <<< $line)
            day=$(cut -d' ' -f4 <<< $line)

            printf "%s 45%s (%s %s)\n" "$id" "$ip" "$mon" "$day"
done <<< "$inputFile"

I tried this

result+=$("%s 45%s (%s %s)\n" "$id" "$ip" "$mon" "$day")

and then just doing

less "$result"

outside the loop but that didn't work and I don't know where to go from here.

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

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

发布评论

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

评论(2

酒中人 2025-01-30 15:34:15

*NIX全都与流工具有关。一种工具流到另一个工具。如果您想观看少量运行脚本,您可以“管道”它...

将脚本写入文件 - 我们将其称为“ my.script.sh”,使其可执行.sh“然后使用| (管道)操作员...

./my.script.sh | less

*nix is all about streaming tools. One tools streams to another. if you want to watch your script run using less you can just 'pipe' it...

write your script to a file - we'll call it "my.script.sh", make it executable "chmod +x my.script.sh" then use the | (pipe) operator...

./my.script.sh | less
无可置疑 2025-01-30 15:34:15

正如戈登所说,这是很多毫无意义的工作,尤其是如果文件将很大。也&lt;&lt;&lt; “ $ inputfile”表示您所说的文件的整个文本,已经读取了inputfile变量,除非有inputfile变量一些具体原因?

尝试这样的事情 -

awk '{printf "%s 45%s (%s %s)\n", $1, $2, $3, $4}' file.in | less

或者如果您希望将输出保存在文件中并分页到屏幕上,

awk '{ printf "%s 45%s (%s %s)\n", $1, $2, $3, $4 }' file.in | tee out.txt | less
  

我也同意Clint。分页可能应该是分开的 - 但是,由于我还没有看到整个结构,请尝试一些,看看什么有效。

As Gordon said, this is a lot of pointless work, especially if the file is going to be big. Also <<< "$inputFile" implies the entire text of the file you said was so big has already been read into the inputFile variable, which seems pointless and counterproductive unless there's some specific reason?

Try something like this -

awk '{printf "%s 45%s (%s %s)\n", $1, $2, $3, $4}' file.in | less

or if you want the output saved in a file AND paginated to the screen,

awk '{ printf "%s 45%s (%s %s)\n", $1, $2, $3, $4 }' file.in | tee out.txt | less
  

I also agree with Clint. The pagination should probably be separate - but since I haven't seen the whole structure, try a few and see what works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文