【问题标题】:Catenate files with blank lines between them [duplicate]将文件与它们之间的空行连接起来[重复]
【发布时间】:2016-05-27 04:51:11
【问题描述】:

我们如何将给定目录中所有文件的所有内容复制到一个文件中以便每个文件的内容之间有两个空行

不用说,我是 bash 脚本的新手,我知道这不是一个特别复杂的代码!

任何帮助将不胜感激。

相关链接如下:
* How do I compare the contents of all the files in a directory against another directory?
* Append contents of one file into another
* BASH: Copy all files and directories into another directory in the same parent directory

看完cmets,我最初的尝试是这样的:

cat * > newfile.txt  

但这不会在每个新文件的内容之间创建两个空行。

【问题讨论】:

  • cat 是你的朋友,请阅读手册页。
  • cat * > newfile.txt..
  • 你能解释为什么你回滚我的标题编辑吗?您将继续收到带有当前标题的cat * 答案。
  • @tripleee 我尝试更正多余的空格,如果您再次编辑问题,我们将非常欢迎!
  • @tripleee 删除重复的问题更好,或者我能做什么?

标签: bash for-loop


【解决方案1】:

试试这个。

awk 'FNR==1 && NR>1 { printf("\n\n") }1' * >newfile.txt

变量FNR 是当前文件中的行号,NR 是整个行号。

【讨论】:

    【解决方案2】:

    一种方式:

    (
        files=(*)
        cat "${files[0]}"
        for (( i = 1; i < "${#files[@]}" ; ++i )) ; do
            echo
            echo
            cat "${files[i]}"
        done
    ) > newfile.txt
    

    【讨论】:

    • 这对我不起作用,newfile.txt 只有第一个文件的内容。
    • 这里打错了,for这一行的中间部分应该是i &lt; "${#files[@]}"--注意#获取数组的元素个数。
    • @tripleee 现在成功了!
    • @tripleee:糟糕,现在修复了。谢谢!
    【解决方案3】:

    文件组织示例:

    我有一个目录 ~/Pictures/Temp

    如果我想将 PNG 从那个目录移动到另一个目录,我首先想为我的文件名设置一个变量:

    # This could be other file types as well
    file=$(find ~/Pictures/Temp/*.png)
    

    当然有很多方法可以查看这个结帐:

    $ man find
    $ man ls
    

    然后我想设置一个目录变量(特别是如果这个目录将是一个日期之类的东西

    dir=$(some defining command here perhaps an awk of an ls -lt)
    # Then we want to check for that directories existence and make it if
    # it doesn't exist
    [[ -e $dir ]] || mkdir "$dir"
    # [[ -d $dir ]] will work here as well
    

    你可以写一个 for 循环:

    # t is time with the sleep this will be in seconds
    # super useful with an every minute crontab
    for ((t=1; t<59; t++));
    do
        # see above
        file=$(blah blah)
        # do nothing if there is no file to move
        [[ -z $file ]] || mv "$file" "$dir/$file"
        sleep 1
    done
    

    如果有任何不清楚的地方请谷歌一下,这里有一些有用的链接: https://www.gnu.org/software/gawk/manual/html_node/For-Statement.html http://www.gnu.org/software/gawk/manual/gawk.html

    此页面上的最佳链接如下:

    http://mywiki.wooledge.org/BashFAQ/031

    编辑:

    无论如何,我的整个答案是,您可以轻松编写一个脚本,将系统上的某些文件组织 60 秒,然后编写一个 crontab 来自动为您组织:

    crontab -e 
    

    这是一个例子

    $ crontab -l
    * * * * * ~/Applications/Startup/Desktop-Cleanup.sh
    # where ~/Applications/Startup/Desktop-Cleanup.sh is a custom application that I wrote 
    

    【讨论】:

    • 呃,这充其量似乎是切线的。
    • 我有多动症……我的错
    • 也许你会想创建一个新问题,这实际上是一个主题作为答案。恐怕它很可能从这里被删除。
    • 怎么样? Bhishan Poudel 正在谈论使用 bash 脚本来组织文件。我给了他一个例子,说明如何在不为他编写脚本的情况下做到这一点。
    • @tripleee 我明白你在说什么,公平地说我误读了问题的第一次迭代。你应该看看 rev 1。这似乎是一个完全不同的问题。
    猜你喜欢
    • 2011-03-26
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多