将多行输出重定向到多个文件 [英] Redirecting multiline output to multiple files

查看:107
本文介绍了将多行输出重定向到多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL列表,并且想确定什么是目录,什么不是:

I have a list of URLs, and would like to identify what is a directory and what is not:

https://www.example.com/folder/
https://www.example.com/folder9/
https://www.example.com/folder/file.sh
https://www.example.com/folder/text

我可以使用grep -e /$查找哪一个,但是我想执行一个内联命令,在该命令中我可以根据该逻辑重定向输出.

I can use grep -e /$ to find which is which, but I'd like to do an inline command where I can redirect the output based on that logic.

我知道awk在这里可能有答案,但是没有足够的awk经验来做到这一点.

I understand that awk may have the answer here, but don't have enough experience in awk to do this.

类似的东西:

cat urls | if /$ matches write to folders.txt else write to files.txt

我可以将所有内容放到一个文件中,然后读取两次,但是当到达数千行时,我觉得效率很低.

I could drop it all to a file then read it twice but when it gets to thousands of lines I feel that would be inefficient.

推荐答案

是的,awk是一个不错的选择:

Yes, awk is a great choice for this:

awk '/\/$/ { print > "folders.txt"; next }
           { print > "files.txt" }' urls.txt

  • /\/$/ { print > "folders.txt"; next }如果该行以/结尾,则将其写入folder.txt,然后跳至下一行
  • { print > "files.txt" }将所有其他行写入files.txt
    • /\/$/ { print > "folders.txt"; next } if the line ends with a /, write it to folders.txt and skip to the next line
    • { print > "files.txt" } write all other lines to files.txt
    • 如果文件中有尾随空格,则可能要使用表达式/\/[[:space:]]*$/而不是/\/$/.

      You may want to use the expression /\/[[:space:]]*$/ instead of /\/$/ in case you have trailing spaces in your file.

      这篇关于将多行输出重定向到多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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