庆典&安培; AWK:遍历目录上运行的所有文件两个独立的awk命令,并在新的目录保存 [英] bash & awk: Loop through dir running two separate awk commands on all files and saving in new dir

查看:132
本文介绍了庆典&安培; AWK:遍历目录上运行的所有文件两个独立的awk命令,并在新的目录保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问了一个问题,并获得美妙的帮助: http://goo.gl/HfovmX

I asked a question yesterday and received wonderful help: http://goo.gl/HfovmX

我觉得我越来越用awk来解决问题的窍门,但我现在需要自动完成一些工作,希望我能使用bash和awk做到这一点。

I think I am getting the hang of using awk to solve the problem but I now need to automate some of the work and hope I can do this with bash and awk as well.

要从另一个线程回顾一下:

To recap from the other thread:

我使用的是Mac和有没有独特的标识符追平纪录彼此一堆文本文件。绑在一起的唯一方法是通过注意在文本文件中的位置,并导入到统计软件包之前跟他们打交道。

I am using a Mac and have a bunch of text files with no unique identifier tying records to each other. The only way to tie them together is by noting the position in text files and dealing with them before importing into a stats package.

解决方案code是:

awk '/^AB1/{ab1=$0;next}/^AB2/{print $1,$2,ab1}' file01.txt > newfile01.txt

我是有附加文件名在输出文件来定位$ 7发送的,所以我跑了第二awk命令和它的工作:

I was having issues appending the filename to position $7 in the output file, so I ran a second awk command and it worked:

awk '{print $1,$2,$3,$4,$5,$6,FILENAME}' newfile01.txt > newnewfile01.txt

我想能够做的就是在目录全这些文件指向的脚本。这将理想上的所有* .TXT同时运行上述命令,然后或者保存到一个新目录中保持相同的文件名(如果容易)或保存到同一目录与新的文件名(如:prePEND新该文件名)。

What I would like to be able to do is point the script at the directory full of these files. It would ideally run both of the above commands on all *.txt and then save either to a new directory maintaining the same filename (if easier) or saving to the same directory with a new filename (ex: prepend 'new' to the filename).

对我来说,最终的结果是,我将猫所有的新文件合并成一个巨大的txt文件,并导入到数学程序。此导入文件现在有文件名来帮助我们的ID,我们在第一时间拿到了行,我们将所有的信息在同一行/列绑在一起的记录,所以我们可以分析一下。

The end result for me is that I will cat all of the new files into one massive txt file and import into the math programme. This imported file will now have the filename to help us ID where we got the row in the first place and we will have all information tying the records together on a single line/row, so we can analyze.

感谢您事先的任何帮助/指导。

Thank you advance for any help/guidance.

推荐答案

修改您提出的解决方案,以便它现在通过在当前目录下的* txt文件迭代:

Modifying your proposed solution so that it now iterates through the *txt files in the current directory:

for f in *txt ; do awk '/^AB1/{ab1=$0;next}/^AB2/{print $1, $2, ab1}' "$f" > "new$f"; awk '{print $1,$2,$3,$4,$5,$6,FILENAME}' "new$f" > "newnew$f"; done

但我怀疑你想要的第一个文件,而不是第二个文件的文件名:

But I suspect you want the filename of the first file, not the second file:

for f in *txt ; do awk '/^AB1/{ab1=$0;next}/^AB2/{print $1, $2, ab1, FILENAME}' "$f" > "new$f"; done

最后,第一个解决方案的以下多层线路版将帮助您了解发生了什么:

Finally, the following multi-line version of the first solution will help you understand what's going on:

for f in *txt
do
    awk '/^AB1/{ab1=$0;next}/^AB2/{print $1, $2, ab1}' "$f" > "new$f"
    awk '{print $1,$2,$3,$4,$5,$6,FILENAME}' "new$f" > "newnew$f"
done

您可以根据您的具体要求尝试这些并对其进行修改。

You can try these and modify them according to your specific requirements.

这篇关于庆典&安培; AWK:遍历目录上运行的所有文件两个独立的awk命令,并在新的目录保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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