bashawksedgreplogfiles

Output only parts from a logfile (a function name and a param value of it)


I dont have find a question like for this specific case, I have a logfile like this:

"foo function1 para1=abc para2=def para3=ghi bar 
foo function2 para1=jkl para2=mno para3=pqr bar"

Now i want execute a one-liner on a gnu bash with this output:

function1 def
function2 mno

foo indicates the start for the function name and bar is the sign for the end of this block. So i want to search for the word "foo", extract the next word (the function name) and then search for the param2 and extract only the value.

How can I do this with a one-liner (not a script)?


Solution

  • @Simi: Try:

    awk -F'[ ="]' '{for(i=1;i<=NF;i++){if($i=="foo"){printf("%s",$(i+1))};if($i=="para2"){printf(" %s\n",$(i+1))}}}'  Input_file
    

    Here I am making field separator as space or = or ("), then I am traversing into all the fields of a line then searching for strings(foo,para2) if any field has these values then simply printing the next field's values as per your requirement. Let me know if this helps you.