awk - if field is empty, move line to new file

I have a script with this statement:

/usr/xpg4/bin/awk -F"[,|]" 'NR==FNR{s["\"" $1 "\""]=$2;next}{printf "%s\"%s\"\n", $0, s[$3]}' LOOKUP.TXT finallistnew.txt >test.txt

I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional file.

Any ideas?
Thanks,

!$3 { print > newfile }

or

!$3 { print > newfile; next }

As you didn't give any input data sample, this is meaningless if there is a forth field!!

(and as you didn't use code-tags - or carriage returns, I didn't check whether you referenced $4)

Sorry. I don't know what code tags are in Unix scripting but here is a sample data file:

"16","[\\server\IP\SCAN\11119431.pdf","100-1115555-222","
00001","Docs","SMITH, JO","DC 1","FL","Administrator
","DK","DK","22222"

"16","[URL="file://\\\\server\\IP\\SCAN\\88888892.pdf","101-2221140-009"]\\server\IP\SCAN\88888892.pdf","101-2221140-009](file://\\server\IP\SCAN\11119431.pdf","100-1115555-222)","
00001","Docs","LMN. Enterprises Inc.","DC 1","AK","Administ
rator","DK","DK",""

Each line above is one line. So there are 2 lines in the data file, for example. But in the example above, if the last field in each line does not contain a value, I want it to kick out to another file and remove it from the data file above. So in this example, the second line would be moved to a new file.

Hope that makes sense.

Code-tags are a forum thing, they make what you post more readable.

So, I'll do that first, then I can read it...

/usr/xpg4/bin/awk -F"[,|]" '
  NR==FNR{s["\"" $1 "\""]=$2; next
}

{
  printf "%s\"%s\"\n", $0, s[$3]
}
' LOOKUP.TXT finallistnew.txt >test.txt

"16","\\server\IP\SCAN\11119431.pdf","100-1115555-222","
00001","Docs","SMITH, JO","DC 1","FL","Administrator
","DK","DK","22222"

"16","\\server\IP\SCAN\88888892.pdf","101-2221140-009","
00001","Docs","LMN. Enterprises Inc.","DC 1","AK","Administ
rator","DK","DK",""

Code tags have nothing to do with scripting; they are what you put around code in posts to this forum (as I have done to your code below).

Carriage returns are not necessary, but newlines are useful when posting code; they make it much more readable (as do spaces and indentation):

/usr/xpg4/bin/awk -F"[,|]" '
  NR == FNR { s["\"" $1 "\""] = $2
  next
}
{
  printf "%s\"%s\"\n", $0, s[$3]
}' LOOKUP.TXT finallistnew.txt >test.txt

Then put it on one line.

if ( $NF == "" ) print > newfile
else print

I have a couple of additional questions.

You pass two files into AWK. Which file provides the data you show? Is it from both files? If not, what's in the other file?

Where would you like the output from this to go?:

{
 printf "%s\"%s\"\n", $0, s[$3]
}

So if this code produces the data file above:
/usr/xpg4/bin/awk -F"[,|]" 'NR==FNR{s["\"" $1 "\""]=$2;next}{printf "%s\"%s\"\n", $0, s[$3]}' LOOKUP.TXT finallistnew.txt >test.txt

How do you insert a if statement into that to make it kick out? I can also include a line after this code to remove the line if there is no value in the last field.

---------- Post updated at 06:56 PM ---------- Previous update was at 06:54 PM ----------

scottn,

Yes it is taken from two data files. Where would I like the data to go? I would like the line that does not contain a value in the last field to go to a file called /tmp/deleteme.

Thanks,

Please excuse my confusion, but after clearing that up it's clear that the last field is not the third field.

A clause of the effect:

!$NF { print > "test.txt" }

should work

Scottn, Can you add the if statement to my existing statement? I am not sure that can be done.

My current statement:
/usr/xpg4/bin/awk -F"[,|]" 'NR==FNR{s["\"" $1 "\""]=$2;next}{printf "%s\"%s\"\n"
, $0, s[$3]}' LOOKUP.TXT finallistnew.txt >test.txt

Thank you.

That's not a statement. It's a program!

It's not against the law to display code nicely, and as I know the local Steueramt won't tax you on it!

/usr/xpg4/bin/awk -F"[,|]" '
!$NF { print > "test.txt"; next }
NR==FNR {s["\"" $1 "\""]=$2; next}
{
  printf "%s\"%s\"\n", $0, s[$3]
}
' LOOKUP.TXT finallistnew.txt >test.txt

I would guess there, but I have a headache from reading your unformatted post.

As all input (that looks the same) comes from both files, I guess you want to apply the rule to all files.