0

I have some text files as follows

293  800 J A              0   0  162
294  801 J R        -     0   0   67  
295  802 J P        -     0   0   56
298  805 J G  S    S-     0   0   22 
313  820 J R  T  4 S-     0   0  152 

I would like to print column4 if column5 is empty.

desired output

>filename
ARP

I used the following code. But this code prints only the filenames.

awk '{ 
if (FNR == 1 ) print ">" FILENAME 
  if ($5 == "") {
printf $4 

 }
} 

END { printf "\n"}' *.txt
2
  • in your example it indeed looks like there is more than 1 space between the first and the second column; please check that and adjust the data field you want accordingly (thats only possible if the number of spaces is constant; if not you have to use FIELDWIDTHS) - when i select your example i get 2 spaces; if thats the case it should be $6, not $5; and $5 instead of $4
    – Seismoid
    Oct 14, 2012 at 14:19
  • What character(s) separate your "columns"? Remember that by default awk treats any contiguous sequence of white space characters as a field separator so if your fields (columns) are separated by sequences of white space characters then there's no way for awk to tell when a field is missing unless your fields are fixed width and then you can use substr() or gawk's FIELDWIDTHS or similar to identify where the fields lie.
    – Ed Morton
    Jul 17, 2021 at 16:35

4 Answers 4

2

Here's one way using GNU awk:

awk 'BEGIN { FIELDWIDTHS="5 4 2 3 3 2 7 4 3" } FNR==1 { print ">" FILENAME } $5 == "   " { sub(/  $/, "", $4); printf $4 } END { printf "\n" }' file.txt

Result:

>file.txt
ARP
2
  • @steve.Thank you very much for your answer. I tried your code.It doesn't print column4. It prints only filenames. Oct 14, 2012 at 14:48
  • @user1606717: You may need to fiddle with the fieldwidths. Since awk doesn't understand empty fields, you need to set FIELDWIDTHS so that awk can understand what a field looks like. In future, I would highly recommend outputting your data with a delimiter other than space (if possible). I've tested the code on the sample file you describe.
    – Steve
    Oct 14, 2012 at 15:00
1

This is not an elegant solution by any means and it is specific to this file.

You can do something like this

cut -c1-15 yourtext | awk '$5 {print $4}'

where 15 is the number of characters including column 5.

I do strongly agree with steve's suggestion to use an better alternative for your files. Or at least put a dummy/error value instead of leaving columns blank.

1
awk '{if(substr($0,15,1)~/ /)printf("%s",$4);}' your_file

tested below:

> cat temp
293  800 J A              0   0  162
294  801 J R        -     0   0   67  
295  802 J P        -     0   0   56
298  805 J G  S    S-     0   0   22 
313  820 J R  T  4 S-     0   0  152
> awk '{if(substr($0,15,1)~/ /)printf("%s",$4);}' temp
ARP>
0

This is a starting point assuming the variations in column numbers stay the same.

awk '$5 !="" && NF<=8 {printf $4}END{print "\n"}' data.txt

yields

ARP

you can graft on the parts to display the filename.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.