Sample Header Ad - 728x90

Pass output of command line to awk variable

0 votes
3 answers
98 views
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading [these](https://unix.stackexchange.com/questions/475008/command-line-argument-in-awk) [questions](http://mywiki.wooledge.org/BashFAQ/082) , I thought this could work:
-shell
awk -v num=$(wc -l my_first_file.bed) '{print $1, $2, $3, $4/num}' my_other_file.bed
but it throws this error:
awk: cmd. line:1: my_first_file.bed
awk: cmd. line:1:              ^ syntax error
Protecting the . with a backslash doesn't change anything, nor does using backticks instead of $(). How can I use the output of wc -l as an awk variable? This will all be happening inside a snakemake pipeline, so I am somewhat limited in terms of flexibility. Contents of my_other_file.bed:
chrUn_KI270548v1	0	50	0.00000
chrUn_KI270548v1	50	192	1.00000
chrUn_KI270548v1	192	497	0.00000
chrUn_KI270548v1	497	639	1.00000
chrUn_KI270548v1	639	723	0.00000
chrUn_KI270548v1	723	860	1.00000
chrUn_KI270548v1	860	865	2.00000
chrUn_KI270548v1	865	879	1.00000
chrUn_KI270548v1	879	991	2.00000
chrUn_KI270548v1	991	1002	3.00000
chrUn_KI270548v1	1002	1021	2.00000
chrUn_KI270548v1	1021	1093	1.00000
chrUn_KI270548v1	1093	1133	2.00000
chrUn_KI270548v1	1133	1222	1.00000
chrUn_KI270548v1	1222	1235	2.00000
chrUn_KI270548v1	1235	1364	1.00000
chrUn_KI270590v1	0	16	4.00000
chrUn_KI270590v1	16	46	5.00000
chrUn_KI270590v1	46	48	6.00000
chrUn_KI270590v1	48	95	7.00000
chrUn_KI270590v1	95	117	8.00000
chrUn_KI270590v1	117	130	9.00000
chrUn_KI270590v1	130	136	8.00000
chrUn_KI270590v1	136	138	7.00000
chrUn_KI270590v1	138	139	6.00000
Asked by Whitehot (245 rep)
Jun 20, 2024, 09:21 AM
Last activity: Jun 20, 2024, 01:19 PM