not enough arguments to satisfy format string - awk

What is wrong with this, please?
awk '{printf "%10.5f %6.4f %6.4f %6.4f %6.4f R\n, $1, $4, $5, $7, $8"}' R.dat > R0
It gives the error: not enough arguments to satisfy format string
There are five % to format 5 $columns.

You should do it like following manner. Where we need to enclose all formatting parameters inside "...." and then other section(after ,) we need to put whatever field values/variables etc are the ones which we want to print in current line.
awk '{printf "%10.5f %6.4f %6.4f %6.4f %6.4f R\n", $1, $4, $5, $7, $8}' R.dat > R0
OR to make it more clear use like:
awk '{printf("%10.5f %6.4f %6.4f %6.4f %6.4f R\n", $1, $4, $5, $7, $8)}' R.dat > R0

Related

Delimited columns in linux with awk

I have a file (df.txt) with 2 fields, but the first field has different size:
HOL99001 121112120001121122020112122010202121010100022002021................
804B 121202011100121121200010200120202220111200011002010....................
WW9 212202021000022111111111201001110001111200020200211.....................
I use a program that requires that the second field must start at the same position in different lines, thus:
HOL99001 121112120001121122020112122010202121010100022002021
804B 121202011100121121200010200120202220111200011002010
WW9 212202021000022111111111201001110001111200020200211
I am using
awk '{print $1.8, $2}' df.txt > dfinal.txt
You could use:
gawk '{printf("%8s %s\n", $1, $2)}' df.txt
HOL99001 121112120001121122020112122010202121010100022002021
804B 121202011100121121200010200120202220111200011002010
WW9 212202021000022111111111201001110001111200020200211
or:
gawk '{printf("%-8s %s\n", $1, $2)}' df.txt
HOL99001 121112120001121122020112122010202121010100022002021
804B 121202011100121121200010200120202220111200011002010
WW9 212202021000022111111111201001110001111200020200211

Understanding how OFS works in AWK

This is a follow-up to my question to understand more about the OFS in AWK.
My understanding is, set it once in the beginning and it will be used in "print" to separate the fields. However, it didn't work as expected, as explained in my original question.
My File: someone.txt
LN_A,FN_A<aa#xyz.com>;
LN_B,FN_B<bb#xyz.com>;
Expected output:
FN_A,LN_A,aa
FN_B,LN_B,bb
I have tried the following:
awk -F'[,<#]' -v OFS=',' '{print $2 $1 $3}' someone.txt
awk -F'[,<#]' -v OFS=',' 'NF=3 {print $2 $1 $3}' someone.txt
awk -F'[,<#]' -v OFS=',' 'NF=3; {print $2 $1 $3}' someone.txt
awk -F'[,<#]' -v OFS=',' '{$1=$1} {print $2 $1 $3}' someone.txt
awk -F'[,<#]' -v OFS=',' '{$1=$1} {print $0}' someone.txt
Finally, I managed to get the required output with the following:
awk -F'[,<#]' '{print $2 "," $1 "," $3}' someone.txt
Consider these cases:
a) $ echo '1 2 3' | awk '{print}'
1 2 3
b) $ echo '1 2 3' | awk '{print $1, $2, $3}'
1 2 3
c) $ echo '1 2 3' | awk -v OFS=',' '{print}'
1 2 3
d) $ echo '1 2 3' | awk -v OFS=',' '{print $1, $2, $3}'
1,2,3
e) $ echo '1 2 3' | awk -v OFS=',' '{$1=$1; print}'
1,2,3
The above show OFS being used in "b" and "d" (when individual fields are being printed in a comma-separated list) and in "e" (when the record $0 is being reconstructed as a result of a value being assigned to a field before the record is printed).
Those are the only 2 times when OFS is used implicitly - when printing a comma-separated list of values and when reconstructing the record.
When you print the record (e.g. by print or print $0) as in "a" and "c" above or print any other string you are not using OFS. OFS may have been used earlier to reconstruct the record as in "e" above but the act of printing anything that's not a comma-separated list is not using OFS, it's just printing any old string which just happens to be $0 in this case.
Note:
Explicitly changing a field reconstructs $0 from the existing fields using OFS between the fields, it does not resplit $0 into fields again so FS is not used in this process. So $1=$1 or sub(/1/,2,$1) uses OFS but not FS.
Explicitly changing $0 (i.e. not implicitly as a result of 1 above) resplits $0 into fields using FS as the separator, it does not use OFS in any way. So $0=$0 or sub(/1/,2) uses FS but not OFS.
Understanding how FS and OFS work together and how they effect assignments to fields and $0 is very important. If you can explain this behavior then you've got it:
f) $ echo 'a b' | awk -v OFS=',' '{print NF, $0, $1, $2}'
2,a b,a,b
g) $ echo 'a b' | awk -v OFS=',' '{$1=$1; print NF, $0, $1, $2}'
2,a,b,a,b
h) $ echo 'a b' | awk -v OFS=',' '{$1=$1; $0=$0; print NF, $0, $1, $2}'
1,a,b,a,b,
i) $ echo 'a b' | awk -v OFS=',' '{$1=$1; $0=$0; FS=OFS; print NF, $0, $1, $2}'
1,a,b,a,b,
j) $ echo 'a b' | awk -v OFS=',' '{$1=$1; $0=$0; FS=OFS; $1=$1; print NF, $0, $1, $2}'
1,a,b,a,b,
k) $ echo 'a b' | awk -v OFS=',' '{$1=$1; $0=$0; FS=OFS; $1=$1; $0=$0; print NF, $0, $1, $2}'
2,a,b,a,b
If not then feel free to ask questions.
It is simple, you have set the OFS="," in beginning of your awk statement but you are simply printing the fields(NOTE: without editing the line OR without mentioning field separator(using comma etc)) in that case OFS will not come in picture that is why your output is NOT having anything like separator.
awk -F'[,<#]' -v OFS=',' '{print $2,$1,$3}' Input_fie
If you use above command where I have mentioned , between printing fields you will see you are getting OFS now and this is how it works.
Or in case you want to see use of OFS you could use this(though above solution is BEST one but for your understanding I am adding this one too).
awk -F'[,<#]' -v OFS=',' '{$0=$2 OFS $1 OFS $3} 1' Input_file
Example to understand OFS by printing whole line(s): Let us understand it more clearly by printing whole line with OFS and withoutOFS` effect.
Let us run this code:
awk -F'[,<#]' -v OFS=',' 'FNR==1{$1=$1} 1' Input_file
What it does is when line number 1 is there then I am resetting $1's value as mentioned above to let OFS come into picture so that new value of OFS comes(off course wherever field separator was picked it will place OFS value there). So it will only be done for first line and REST of the lines nothing should happen. Let us see what output comes now?
LN_A,FN_A,aa,xyz.com>;
LN_B,FN_B<bb#xyz.com>;
You see the difference? See first line is having , in output and 2nd line is printing as it is, why because in only 1st line we have edited the first field so OFS came into picture.
As I just found an unused copy of Aho, Kernighan, Weinberger: The AWK Programming language from 1988, I(t)'ll take you to the source (pages 35-36):
"Field Variables. The fields of the current input line are called $1, $2,
through $NF; $0 refers to the whole line. Fields share the properties of other
variables — they may be used in arithmetic or string operations, and may be
assigned to. - -
One can assign a new string to a field:
BEGIN { FS = OFS = "\t" }
$4 == "North America" { $4 = "NA" }
$4 == "South America" { $4 = "SA" }
{ print }
In this program, the BEGIN action sets FS, the variable that controls the input
field separator, and OFS, the output field separator, both to a tab. The print
statement in the fourth line prints the value of $0 after it has been modified by
previous assignments. This is important: when $0 is changed by assignment or
substitution, $1, $2, etc., and NF will be recomputed; likewise, when one of $1, $2, etc., is changed, $0 is reconstructed using OFS to separate fields."

awk to format file using a specific order

I am trying to format a tab-delimited file using awk and the command runs but no output results. The output is also tab-delimited. The format of the output is $1 $2 $2 $3 REF=$4;OBS=$5 $6. Maybe the awk is not the best approach as it seems like it should work. Thank you :).
file (~370 lines all in the below format)
chr4 70501545 rs28560191 C A UGT2A1;UGT2A2
desired output
chr4 70501545 70501545 rs28560191 REF=C;OBS=A UGT2A1;UGT2A2
awk
awk -F'\t' -v OFS='\t' '{print $1,$2,$2,$3,"REF="$4";""OBS="$5,$6}' file
You are forgetting the print statement.
awk '{ print $1 "\t" $2 "\t" $2 "\t" $3 "\t" "REF="$4";""OBS="$5 "\t" $6}' file

Convert a decimal data field to hexadecimal using sed or awk

Who can correct this command to get the desired output :
input : "1|2|30|4"
echo "1|2|30|4" | awk -F, -v OFS=| '{print $1,$2; printf "%04X", $3; print $4}'
Output expected :
1|2|001E|4
Best regards.
$ echo "1|2|30|4" |
awk -F'|' -v OFS='|' '{print $1, $2, sprintf("%X", $3), $4}'
1|2|1E|4
echo "1|2|30|4" | awk -F"|" '{printf "%s|%s|%04X|%s", $1, $2, $3, $4}'
Output:
1|2|001E|4

Why is awk's OFS only applied to the first line?

I'm doing some simple calculations on a file using awk, but cannot get the output formatting right. The OFS is for some reason only applied to the first line (i.e. only within the BEGIN block), and for other rows a single space is inserted between fields.
Input:
title c1 c2 c3 n
AA 14 6 3 40
BB 8 2 2 38
Oneliner:
cat file.txt | awk -F'\t' 'BEGIN {OFS="\t"; print "Title","Freq1","Freq2","Freq3","Total"}; NR>1{printf "%s %.3f %.3f %.3f %d\n", $1, $2/$5, $3/$5, $4/$5, $5;}' > file2.txt
I've tried removing the header from BEGIN but this does not make a difference, and neither does BEGIN{FS="\t";OFS="\t";...}. I'm using awk in cygwin.
Since you're using printf you are actively avoiding OFS entirely. If you want to incorporate OFS, it gets ugly:
NR>1 {printf "%s%s%.3f%s%.3f%s%.3f%s%d\n", $1, OFS, $2/$5, OFS, $3/$5, OFS, $4/$5, OFS, $5}
Or, don't use printf:
NR>1 {print $1, sprintf("%.3f",$2/$5), sprintf("%.3f",$3/$5), sprintf("%.3f",$4/$5), $5}