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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - if field is empty, move line to new file
# 8  
Old 10-07-2009
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:
Code:
!$NF { print > "test.txt" }

should work
# 9  
Old 10-07-2009
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.
# 10  
Old 10-07-2009
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!

Code:
/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.

Last edited by Scott; 10-07-2009 at 08:49 PM.. Reason: added next to !$NF line. That might fix or break it or neither...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

How to detect empty field in awk ?

Hi ! programmers I have a need of detecting empty field in file my file looks like this 40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31... (7 Replies)
Discussion started by: Dona Clara
7 Replies

5. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

6. UNIX for Dummies Questions & Answers

An easier way to move character from field to a new line

Hi ! Is there a more convenient way to do the following: When, in $2, there is more then one letter (separated by ","), it returns the letter on the second position to a new line in $2 with the same content in $1 and $3 as the line this letter comes from. infile: aaaaa A,B 1,2,3,4,5... (6 Replies)
Discussion started by: lucasvs
6 Replies

7. Shell Programming and Scripting

use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this count,1977,1978,1979 usa, , , blue japan, red, yellow,green india, , yellow,blue china, blue, yellow, green I want the output to be(replace everything, including empty data, with the most recent data): ... (1 Reply)
Discussion started by: grossgermany
1 Replies

8. UNIX for Dummies Questions & Answers

Help required on printing the line if third field is not a empty space

Hi Experts, I want to print the lines whose third field in non-empty/blank space. i.e. INPUT FILE/B] dcdccghjrry0yd cont dcdccttrk820529 cont rdekedfsSCr dcdccttrdky2d0y cont rdekedfsSC2 ... (3 Replies)
Discussion started by: krao
3 Replies

9. Shell Programming and Scripting

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question