Bash脚本更改每个文件的输出文件 [英] Bash Script changing the output file for every file

查看:49
本文介绍了Bash脚本更改每个文件的输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在文件夹中的每个文件.log上使用命令,但是我不明白如何更改文件夹中每个文件的输出文件.

I'm trying to use a command on everyfile .log in a folder but I can't understand how to change the output file for every file in the folder.

#!/bin/bash
N="0"
for i in "*.log"
do
    echo "Processing $f file..."
    cat $i | grep "test" | awk '/show/ {a = $1} !/show/{print a,$6}' > "log$N.txt"
done

我如何增加log $ n.txt的计数器?

How can i increment the counter for log$n.txt?

推荐答案

仅出于处理文本的目的编写shell循环是不好的做法(请参阅

It's bad practice to write shell loops just to process text (see https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). This is all you need:

awk '
    FNR==1  { print "Processing", FILENAME; a=""; close(out); out="log" fileNr++ ".txt" }
    !/test/ { next }
    /show/  { a = $1; next }
    { print a, $6 > out }
' *.log

这篇关于Bash脚本更改每个文件的输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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