Sunday, March 13, 2022

[SOLVED] awk split more columns and print first word

Issue

I have comma separated file, I would like to split column 15 to $NF (15th column to last column) with same split condition split($column,a,"-") and print for every splited column a[1]. I cannot do loop over column from nth to last and print for every of them.

awk -F',' -v OFS="\t" '{for(i;$15<i<$NF,i+1);split($i,a,"_"); print ???}' file.csv

example of file printed form 15th column:

NBPF1-chr1-16579269-16579502-MedEx,NBPF1-chr1-16580779-16580863-MedEx,NBPF1-chr1-16581333-16581592-MedEx,NBPF1-chr1-16582457-16582758-MedEx,NBPF1-chr1-16583499-16583796-MedEx

What I expect:

NBPF1,NBPF1,NBPF1,NBPF1,NBPF1,NBPF1

Thank you.


Solution

$ awk '{gsub(/-[^,]*/,"")}1' file
NBPF1,NBPF1,NBPF1,NBPF1,NBPF1

$ sed 's/-[^,]*//g' file
NBPF1,NBPF1,NBPF1,NBPF1,NBPF1

If that's not really all you need then please edit your question to provide more truly representative sample input/output.



Answered By - Ed Morton
Answer Checked By - Timothy Miller (WPSolving Admin)