简体   繁体   English

在linux目录中的所有文件中显示特定行

[英]display specific lines in all files in a directory in linux

I have 200 text files in folder F. I want to see lines 2-4 of all files. 我的文件夹F中有200个文本文件。我想查看所有文件的2-4行。 I have tried something like: 我已经尝试过类似的东西:

$sed -n '2,5'p *.txt

but it only reads the first file. 但它只读取第一个文件。 Can anybody please help? 有人可以帮忙吗?

Furthermore, I might need to send these lines to a new file, something like: 此外,我可能需要将这些行发送到新文件,例如:

$sed -n '2,5'p *.txt>path

My knowledge of linux is basic, so if you have a totally different solution, please be more specific. 我对linux的了解是基础知识,因此,如果您有完全不同的解决方案,请更加具体。

awk 'FNR>1&&FNR<5' *.txt > result.txt

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed -ns '2,4p' *.txt > results.txt

If you just want to capture the results: 如果只想捕获结果:

sed -ns '2,4w results.txt' *.txt

Another way to see and capture the results: 查看和捕获结果的另一种方式:

sed -ns '2,4!b;p;w results.txt' *.txt

See here for the s invocation option. 有关s调用选项,请参见此处

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

相关问题 列出具有特定模式文件名的linux目录和子目录中的所有文件 - list all files in directory and subdirectories in linux with specific pattern filename 在Linux的目录中显示几行文件* - Show few lines of files* in a directory in Linux 如何在Linux中的目录及其子目录中的所有文件中为包含字符串“000”的行添加前缀“#”? - How to add a prefix “#” to lines that contain string “000” in all files in a directory and its sub-directories in linux? 在Linux中检索目录下的特定文件 - Retrieve specific files under a directory in Linux 显示Linux中所有用户的文件/目录权限 - Display file / directory permission for all users in Linux 如何在 Linux C 程序中显示用户指定名称为“ls dir_name”的目录中的所有文件 - How to display all files in directory of which name is specified by the user as “ls dir_name” in Linux C program Linux 命令行:在包含特定文本的目录树中查找具有特定扩展名的所有文件 - Linux command line: Find all files with a certain extension in a directory tree containing specific text 如何使用linux cmd列出linux中所有目录中的所有文件 - How to list all files in all directory in linux using linux cmd 重命名Linux目录和子目录中的所有文件 - Rename All Files in directory and subdirectories Linux 将换行符附加到Linux中目录中的所有文件 - Append Newlines to All Files in a Directory in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM