简体   繁体   English

使用awk从目录中的所有文本文件中提取特定行

[英]Using awk to extract specific line from all text files in a directory

I have a folder with 50 text files and I want to extract the first line from each of them at the command line and output this to a result.txt file. 我有一个包含50个文本文件的文件夹,我想在命令行中从每个文本中提取第一行并将其输出到result.txt文件。

I'm using the following command within the directory that contains the files I'm working with: 我在包含正在使用的文件的目录中使用以下命令:

for files in *; do awk '{if(NR==1) print NR, $0}' *.txt; done > result.txt

When I run the command, the result.txt file contains 50 lines but they're all from a single file in the directory rather than one line per file. 当我运行命令时,result.txt文件包含50行,但是它们全部来自目录中的单个文件,而不是每个文件中的一行。 The common appears to be looping over a single 50 times rather than over each of the 50 files. 共同点似乎是循环了50次,而不是循环了50次。

I'd be grateful if someone could help me understand where I'm going wrong with this. 如果有人可以帮助我了解我在哪里出错了,我将不胜感激。

try this - 尝试这个 -

for i in *.txt;do head -1 $i;done > result.txt

OR 要么

for files in *.txt;do awk 'NR==1 {print $0}'  $i;done > result.txt

Your code has two problems: 您的代码有两个问题:

  1. You have an outer loop that iterates over * , but your loop body doesn't use $files . 您有一个循环遍历*的外循环,但是您的循环主体不使用$files That is, you're invoking awk '...' *.txt 50 times. 也就是说,您要调用awk '...' *.txt 50次。 This is why any output from awk is repeated 50 times in result.txt . 这就是为什么awk的任何输出在result.txt重复50次的原因。

  2. Your awk code checks NR (the number of lines read so far), not FNR (the number of lines read within the current file ). 您的awk代码将检查NR (到目前为止已读取的行数),而不是FNR当前文件中已读取的行数)。 NR==1 is true only at the beginning of the very first file. NR==1仅在第一个文件的开头才为真。

There's another problem: result.txt is created first, so it is included among *.txt . 还有一个问题: result.txt首先创建,因此它包含在*.txt To avoid this, give it a different name (one that doesn't end in .txt ) or put it in a different directory. 为避免这种情况,请给它一个不同的名称(不以.txt结尾的名称)或将其放在另一个目录中。

A possible fix: 可能的解决方法:

awk 'FNR==1 {print NR, $0}' *.txt > result

Why not use head? 为什么不使用头? For example with find: 例如,使用find:

find midir/ -type f -exec head -1 {} \; >> result.txt

If you want to follow your approach you need to specify the file and not use the wildcard with awk: 如果您想采用这种方法,则需要指定文件,而不要在awk中使用通配符:

for files in *; do awk '{if(NR==1) print NR, $0}' "$files"; done > result.txt

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

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