从多个文件中删除线,sed命令 [英] Removing lines from multiple files with sed command
问题描述
因此,免责声明:我是pretty新的使用bash和zsh的,所以有机会的话,答案是非常简单的。然而。我检查previous帖子,但没有找到任何东西。 (编辑:我已经在这两个bash和zsh的shells-同样的问题,尝试这样做)
So, disclaimer: I am pretty new to using bash and zsh, so there is a chance the answer is really simple. Nonetheless. I checked previous postings and couldn't find anything. (edit: I have tried this in both bash and zsh shells- same problem.)
我有很多文件的目录,我试图删除每个文件的第一行。
I have a directory with many files and am trying to remove the first line from each file.
所以说,该目录包含:FILE1.TXT FILE2.TXT file3.txt ...等
So say the directory contains: file1.txt file2.txt file3.txt ... etc.
我使用sed命令(非GNU):
I am using the sed command (non-GNU):
sed -i -e "1d" *.txt
有关某些原因,这只是除去第一文件的第一行。我认为,* .TXT会影响到匹配的目录模式中的所有文件。奇怪的是,它是创建具有-e所附文件重复,但无论是重复的和原来是相同的。
For some reason, this is only removing the first line of the first file. I thought that the *.txt would affect all files matching the pattern in directory. Strangely, it is creating the file duplicates with -e appended, but both the duplicate and original are the same.
我想这与其他命令(例如LS * .TXT),它工作正常。是否有一些关于sed的我失踪?
I tried this with other commands (e.g. ls *.txt) and it works fine. Is there something about sed I am missing?
感谢您提前。
推荐答案
在不同的操作系统,不同版本的 SED
的支持各种参数。
Different versions of sed
in differing operating systems support various parameters.
的 -i
标记为不可用。您可以使用以下 / bin / sh的
语法:
The -i
flag is unavailable. You can use the following /bin/sh
syntax:
for i in *.txt
do
f=`mktemp -p .`
sed -e "1d" "${i}" > "${f}" && mv -- "${f}" "${i}"
done
的FreeBSD(11-CURRENT)SED
的 -i
标志要求延期,即使它是空的。因此,必须写成 SED -i,-e1d中的* .txt
FreeBSD (11-CURRENT) sed
The -i
flag requires an extension, even if it's empty. Thus must be written as sed -i "" -e "1d" *.txt
这看起来看到,如果参数如下 -i
是另一种选择(或可能的命令)。如果是这样,它假定在就地改性。如果这似乎是一个文件扩展名,如 .bak的
,将重命名原始的 .bak的
,然后对其进行修改成原始文件的名称
This looks to see if the argument following -i
is another option (or possibly a command). If so, it assumes an in-place modification. If it appears to be a file extension such as ".bak
", it will rename the original with the ".bak
" and then modify it into the original file's name.
有可能会在其他平台上的其他变化,但这些都是三我手边。
There might be other variations on other platforms, but those are the three I have at hand.
这篇关于从多个文件中删除线,sed命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!