0

My Code is in mid of manipulating two input files.

awk -F'|' -v PARM_VAL="${PARM_VALUE[*]}" '
BEGIN { split(PARM_VAL,pa," ") }
FNR==NR
{
for(i=1;i<=NF;i++)
a[NR,i]=$i;
}

END {printf "second value of SPPIN : "a[2,2]", parm : "pa[2]", File val : " FILENAME "First rec of SPPOUT: " $0 ;printf "\n" } ' SPP_IN SPP_OUT

I am passing parm array to awk, storing first input file in array. Just executed the above command. My first input file is getting displayed without print. Anyway to suppress or avoid it?

5
  • Can you explain what you are trying to do here exactly? You don't seem to process the second (SPP_OUT) file anywhere in that script (well you print the last line from that file in the END block I suppose). You appear to be printing the second field from the second line of the SPP_IN file (after storing the entire file in memory for some reason) and also the second field of the PARM_VALUE array and the name of the last file (SPP_OUT in this case) that was processed. Commented Feb 26, 2015 at 5:26
  • Hi I will compare each column of two files based on PARM_VALUE. Commented Feb 26, 2015 at 5:29
  • Compare what to what? What do you do once you compare them? What do you mean your first file is getting printed in the script in the post? What are you trying to get help with here? Commented Feb 26, 2015 at 5:31
  • My concern here is all records of my first input file is displayed even without print statement. How to avoid that. I am trying to compare first record of both files as per PARM Value. Commented Feb 26, 2015 at 5:34
  • @Madan this is your second question demonstrating that you have no idea of the basic syntax of awk. Get the book Effective Awk Programming by Arnold Robbins and at least read the first couple of chapters and try to write a "Hello World" program to get an idea. Commented Feb 26, 2015 at 12:38

1 Answer 1

1

Don't split FNR == NR and the { of the action.

FNR == NR
{

Put them on the same line instead.

FNR == NR {

awk is seeing FNR==NR as a pattern without an action and using the default action of print.

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

2 Comments

Thanks @Etan Resiner. Working Perfect!! But if i try to print first field of all second file records. I am facing syntax error awk -F'|' -v PARM_VAL="${PARM_VALUE[*]}" ' BEGIN { split(PARM_VAL,pa," ") } FNR==NR{ for(i=1;i<=NF;i++) a[NR,i]=$i; } {print } if (FILENAME == "SPP_OUT") { print FILENAME , $1 } END {printf "second value of SPPIN : "a[2,2] ;printf "\n" } ' SPP_IN SPP_OUT
if statements go inside {action} blocks not at the toplevel. And you seem to have a stray {print} there which will print every line of every processed file (as written).

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.