Append filename as additional column but with modification using awk

Viewed 35

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

3 Answers

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
$ awk 'BEGIN{OFS="\t"; split(ARGV[1],f,".")}
      {print $1,$2,$3,$4,f[1]}' A0631-Somatic-WGS.format.flt.txt
X       120143898       6       88725363        A0631-Somatic-WGS
X       147991648       6       132706871       A0631-Somatic-WGS

or just let OFS take care of all of it :

mawk ++NF OFS= FS='[.][^ \t]+[ \t]*$' 
X   120143898   6   88725363    A0631-Somatic-WGS
X   147991648   6   132706871   A0631-Somatic-WGS

A great use-case for "greedy" regex matching

I suppose you can also do it the RS way :

gawk FS RS='[.][^ \t]+[ \t]*\r?\n'
X   120143898   6   88725363    A0631-Somatic-WGS
X   147991648   6   132706871   A0631-Somatic-WGS
Related