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

why is awk printing only first word in a field?

I have a tabulated .txt file with three fields, the third field often having multiple words:

GO:0000002  Akt3    mitochondrial genome maintenance
GO:0000002  Mef2a   mitochondrial genome maintenance
GO:0000002  Mgme1   mitochondrial genome maintenance
GO:0000002  Mpv17   mitochondrial genome maintenance
GO:0000002  Mrpl15  mitochondrial genome maintenance

I wanted to swap fields 1 and 2 using awk, but when i run the command:

awk 'BEGIN{OFS="\t"}{print $2,$1,$3;}' file.txt

I get:

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

Akt3    GO:0000002  mitochondrial
Mef2a   GO:0000002  mitochondrial
Mgme1   GO:0000002  mitochondrial
Mpv17   GO:0000002  mitochondrial

Why do i not get all the words in the third field, and how to solve this?

Thanks in advance

>Solution :

There are various ways to solve the problem, but the main issue is that the output field separator has been set, but the default input field separator is both tabs & space. Setting them both to tab should give the output you want (or make it more generalizable by just swapping the first two fields & printing the remainder of the line)

awk 'BEGIN{OFS="\t"; FS="\t";} {print $2,$1,$3;}' file.txt
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