1

I have a awk script which parses through a log file the script:

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. So I was wondering how I should do this, wether writing a shell script who does this and executes the awk script. Or is there a way to write this in the awk script (maybe with 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)

After executing the script, it should say something like "Type in the ID" or something like that.

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)

Thanks in advance

1
  • If this is a stand alone awk script Make the last one of the args/files passed to awk the thing you are search for. Set a variable to argv[N] then remove it from argc Commented Oct 28, 2014 at 12:24

2 Answers 2

1

I think that you had the right idea when you said:

writing a shell script who does this and executes the awk script

There are a million ways to do this but I would write a bash script that calls your awk script.

Handle whatever user interaction and filesystem actions there.

You can also generate the awk file from within the bash program using here documents. I will show you that later on today.

One of the great strengths of Linux is "little programs holding hands". Windows' greatest weakness is the concept of one monolith program that does everything.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah already wrote a shell script who does this and it works really well
For completeness, post your solution in your original question then mark it as answered. That will help your fellow programmers when they come behind you looking for the same answer.
1

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.

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.

UPDATE

How about:

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

another, less portable version (gawk extension):

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

3 Comments

I basically only want to get a number in the first column by manual input, the rest is just parsing through a log
So you want to read data from a log file and from the stdin input from the interactive user, right?
Yeah, i want to get a input before it starts reading, the input should be set = a variable and then be listed with the outcome of the parsing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.