将多个文件的第n行读入单个输出 [英] Read the n-th line of multiple files into a single output

查看:51
本文介绍了将多个文件的第n行读入单个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些名为 dump_mydump_0.cfg dump_mydump_250.cfg ,...的转储文件,一直到 dump_mydump_40000.cfg .对于每个转储文件,我想取出第16行,读取它们,然后将它们放入一个文件中.

I have some dump files called dump_mydump_0.cfg, dump_mydump_250.cfg, ..., all the way up to dump_mydump_40000.cfg. For each dump file, I'd like to take the 16th line out, read them, and put them into one single file.

我正在使用 sed ,但是遇到一些语法错误.这是我到目前为止的内容:

I'm using sed, but I came across some syntax errors. Here's what I have so far:

for lineNo in 16 ;
for fileNo in 0,40000 ; do
    sed -n  "${lineNo}{p;q;}" dump_mydump_file${lineNo}.cfg >> data.txt
done

推荐答案

或者,使用(GNU)awk:

Alternatively, with (GNU) awk:

awk "FNR==16{print;nextfile}" dump_mydump_{0..40000..250}.cfg > data.txt

(如果操作正确,我将使用OP中显示的文件名,而不是bash for loop生成的文件名.但是您可以根据需要进行编辑.)

(I used the filenames as shown in the OP as opposed to the ones which would have been generated by the bash for loop, if corrected to work. But you can edit as needed.)

优点是您不需要for循环,也不需要产生160个进程.但这不是一个巨大的优势.

The advantage is that you don't need the for loop, and you don't need to spawn 160 processes. But it's not a huge advantage.

这篇关于将多个文件的第n行读入单个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆