gpt4 book ai didi

linux - 使用 Sed 提取多个文件中的标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:05 24 4
gpt4 key购买 nike

我使用 head -3 从一些我需要显示标题数据的文件中提取标题我这样做了:

head -3 file1 file2 file3 

head -3 * 也可以。

我认为 sed 3 file1 file2 file3 可以工作,但它只提供第一个文件的输出,而不提供其他文件。然后我尝试了 sed -n '1,2p' file1 file2 file3。同样只有第一个文件产生任何输出。我还尝试使用通配符 sed -n '1,2p' filename* 只有第一个文件的输出结果相同。

我阅读的所有内容似乎都应该有效。 sed *文件名*

提前致谢

最佳答案

假设 GNU sed 作为问题被标记为 linux。来自 GNU sed manual

-s --separate By default, sed will consider the files specified on the command line as a single continuous long stream. This GNU sed extension allows the user to consider them as separate files: range addresses (such as ‘/abc/,/def/’) are not allowed to span several files, line numbers are relative to the start of each file, $ refers to the last line of each file, and files invoked from the R commands are rewound at the start of each file.

例子:

$ cat file1
foo
bar
$ cat file2
123
456

$ sed -n '1p' file1 file2
foo
$ sed -n '3p' file1 file2
123
$ sed -sn '1p' file1 file2
foo
123


使用 -i 时,隐含了 -s 选项

$ sed -i '1chello' file1 file2
$ cat file1
hello
bar
$ cat file2
hello
456

关于linux - 使用 Sed 提取多个文件中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com