Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Append filename as additional column but with modification using awk

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading