简体   繁体   English

使用awk或bash在Unix中创建包含0字节的空多个文件?

[英]Create empty multiple files in Unix that contain 0 bytes using awk or bash?

I am trying to create multiple empty files on Unix but I do not know if AWK command can do that or do I have to do it in bash? 我正在尝试在Unix上创建多个空文件,但是我不知道AWK命令是否可以做到这一点,还是我必须在bash中做到这一点? if yes could you help me, please? 如果可以,可以帮我吗?

At this moment I use this command: 现在,我使用以下命令:

seq 2 | awk '{print "" > "empty"i++".txt"}' i=1

however every file that is created is 1 byte. 但是,创建的每个文件都是1个字节。 Is there a way to create 0 byte file using awk or bash? 有没有一种方法可以使用awk或bash创建0字节文件?

How about: 怎么样:

touch $(seq 2)

or: 要么:

touch $(seq 2 | sed 's/$/.txt/')

if you want a suffix? 如果要后缀?

The touch command will create a file if it doesn't exist. 如果该文件不存在, touch命令将创建一个文件。 So you could do: 因此,您可以执行以下操作:

for i in `seq 1 2`; do
    touch empty$i.txt
done

If you don't want to use any other commands like touch , and do it purely using bash commands, you could use the redirection operator > without a command in front of it: 如果您不想使用任何其他命令(例如touch ,而仅使用bash命令来执行此操作,则可以使用重定向操作符>而不在其前面添加命令:

for i in `seq 1 2`; do
    > empty$i.txt
done

For more details see the I/O Redirection chapter in the Advanced Bash-Scripting Guide : 有关更多详细信息,请参阅《 高级Bash脚本指南》中的“ I / O重定向”一章:

> filename    
   # The > truncates file "filename" to zero length.
   # If file not present, creates zero-length file (same effect as 'touch').
   # (Same result as ": >", above, but this does not work with some shells.)

If you really want to do it in awk : 如果您真的想在awk这样做:

seq 2 | awk '{system(">" "empty" FNR ".txt")}'

That's not very clever though because it creates a whole new process for every line that awk reads. 但这不是很聪明,因为它为awk读取的每一行创建了一个全新的过程。

@EdMorton's suggestion is therefore preferable, as he uses awk 's internal printf rather than creating a separate process: 因此@EdMorton的建议是可取的,因为他使用awk的内部printf而不是创建一个单独的过程:

seq 2 | awk '{printf "" > "empty" NR ".txt"}'

You would be better to use this: 您最好使用以下方法:

touch {1..2}.txt

which only creates one process. 仅创建一个过程。 However, that will not reduce to zero the size of any pre-existing files you may have - I don't generally like to assume folk are happy to lose data unless they explicitly say so in their question. 但是,这不会将您可能拥有的任何现有文件的大小减小到零-我通常不希望人们不愿意丢失数据,除非他们在问题中明确表示。

The awk command can indeed create empty files on its own, without spawning something in system() : awk命令确实可以自行创建空文件,而无需在system()产生任何东西:

$ awk 'BEGIN{ printf("") > "test1" }'
$ ls -l test1
-rw-r--r--  1 ghoti  staff  0 Jan  9 09:58 test1
$

Note that each output file is recorded in memory upon use, and that memory is not given back until the output file is closed. 请注意,每个输出文件在使用时都会记录在内存中,并且直到关闭输出文件后才会将内存归还。 You can use the close() function to close a file. 您可以使用close()函数关闭文件。

awk -v n=2 'BEGIN{ for(;n;n--) { f="test"n".txt"; printf("") > f; close(f) } }'

But you by no means need awk for this, the touch works nicely from multiple shells (including bash of course), and the mechanism you use to generate filenames for it are numerous and varied. 但是您绝对不需要 awk,这种touch可以在多个shell(当然包括bash)中很好地工作,并且用于生成文件名的机制多种多样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM