I’m trying to add the filename as a new column to file contents, but also removing sections of the name, all using awk.
Currently using the following code which gets me almost there:
awk -v OFS='\t' '{print $1,$2,$3,$4,FILENAME}' A0631-Somatic-WGS.format.flt.txt
File contents:
X 120143898 6 88725363 A0631-Somatic-WGS.format.flt.txt
X 147991648 6 132706871 A0631-Somatic-WGS.format.flt.txt
I want the filename to only maintain the name, not anything following the first period. So ideal output would look like:
File contents:
X 120143898 6 88725363 A0631-Somatic-WGS
I’m thinking of doing ${FILENAME%.format.flt.txt}, but I can’t get awk to accept this as part of its command. I’m fairly sure this is possible though!
Thanks
>Solution :
You may just use sub on FILENAME to remove anything starting with a DOT:
awk -v OFS='\t' '{fn=FILENAME; sub(/\..+/, "", fn);
print $1,$2,$3,$4,fn}' A0631-Somatic-WGS.format.flt.txt
X 120143898 6 88725363 A0631-Somatic-WGS
X 147991648 6 132706871 A0631-Somatic-WGS