简体   繁体   English

在awk脚本中输入命令行?

[英]Command line input in awk script?

I have a awk script which parses through a log file the script: 我有一个awk脚本,该脚本通过日志文件解析该脚本:

BEGIN{
    print "ID", "vFiler", "Type", "host"
}
/=====/{
    vFiler=$2
    next
}
match($0,/root=[^,]*/){
    n=split(substr($0,RSTART+5,RLENGTH-5),N,/:/)
    for(i=1; i<=n; i++)print vFiler,$1,N[i];
}

Now my question, as you can see there is an ID set at the start, which should meant to be typed in every time before the script runs, since the files have different ID's every time. 现在是我的问题,正如您所看到的,在开始时设置了一个ID,应该在脚本运行之前每次键入ID,因为文件每次都具有不同的ID。 So I was wondering how I should do this, wether writing a shell script who does this and executes the awk script. 所以我想知道我该怎么做,是否编写了一个执行此脚本并执行awk脚本的shell脚本。 Or is there a way to write this in the awk script (maybe with getLine?!)? 还是有办法在awk脚本中编写此代码(也许使用getLine ?!)?

Output I want to achieve is: 我要实现的输出是:

ID,vFiler,Type,host
1,vfiler0,/vol/vol0,fapra8.net
4,vfiler1,/vol/lnxpjmmorena,fcvapd10.net
4,vfiler1,/vol/CSArchive,fcvapd11.net

(in the secound case the ID stays the same because the servers are listed on the same Type) (在第二种情况下,ID保持不变,因为服务器以相同的类型列出)

After executing the script, it should say something like "Type in the ID" or something like that. 执行脚本后,它应显示“键入ID”之类的内容。

If this can't be done within the awk script pls do not make the effort to write an shell script (in general I just want to know how I can do it, but im grateful for every answer) 如果无法在awk脚本中完成此操作,请不要尝试编写Shell脚本(通常,我只想知道我该怎么做,但是对每个答案都很感谢)

Thanks in advance 提前致谢

It is not direct answer to your question, but possibly to your problem. 它不是您问题的直接答案,但可能是您问题的直接答案。 I assume you generate some data, and then you need to process it. 我假设您生成了一些数据,然后需要对其进行处理。 When there is some logic in data you need to preserve, it might be worth to use sqlite via shell instead of generating flat files and then parsing and procesing using tools like awk. 当您需要保留数据中的某些逻辑时,可能值得通过外壳使用sqlite而不是生成平面文件,然后使用诸如awk之类的工具进行解析和处理。

If you worry about performance of gerating data (doing INSERT often instead of simple echo to file) you can echo SQL command to file, and then load bunch of data in single transaction before processing them. 如果您担心整理数据的性能(通常执行INSERT而不是简单地回显到文件),则可以将SQL命令回显到文件,然后在处理单个数据之前将其加载。

UPDATE UPDATE

How about: 怎么样:

read MYVAR && awk -v MYVAR="$MYVAR" '{print MYVAR $0}' in.log

another, less portable version (gawk extension): 另一个不那么便携的版本(gawk扩展):

awk 'BEGIN{ "head -1" | getline MYVAR; print MYVAR }{print MYVAR $0}' in.log

I think that you had the right idea when you said: 我认为您说的话是对的:

writing a shell script who does this and executes the awk script 编写执行此操作并执行awk脚本的shell脚本

There are a million ways to do this but I would write a bash script that calls your awk script. 有上百万种方法可以执行此操作,但是我会编写一个bash脚本来调用您的awk脚本。

Handle whatever user interaction and filesystem actions there. 在此处理任何用户交互和文件系统操作。

You can also generate the awk file from within the bash program using here documents. 您也可以使用here文档从bash程序中生成awk文件。 I will show you that later on today. 今天晚些时候我会告诉你。

One of the great strengths of Linux is "little programs holding hands". Linux的一大优势是“小程序牵手”。 Windows' greatest weakness is the concept of one monolith program that does everything. Windows的最大弱点是一个可以完成所有任务的整体程序的概念。

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

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