JBoy
JBoy

Reputation: 5735

Edit header file with awk

I have a file that is white-space separated value, i need to convert this into:

header=tab separated, 
records=" ; " separated (space-semicolon-space)

what i'm doing now is:

cat ${original} | awk 'END {FS=" "} { for(i=1; i<=NR; i++) {if (i==1) { OFS="\t"; print $0; } else { OFS=";" ;print $0; }}}' > ${new}

But is working only partly, first, it produces millions of lines, while the original ones has about 90000.

Second, the header, which should be modified here:

if (i==1) { OFS="\t"; print $0; }

Is not modified at all,

Another option would be by using sed, i can get that job to be done partially, but again the header remains untouched:

cat ${original} | sed 's/\t/ ;/g' > ${new}

Upvotes: 0

Views: 1158

Answers (1)

Kent
Kent

Reputation: 195049

this line should change all the separator in file

 awk -F'\t' -v OFS=";" '$1=$1' file

this will leave header untouched:

 awk -F'\t' -v OFS=";" 'NR>1{$1=$}1' file

this will only change the header line:

 awk -F'\t' -v OFS=";" 'NR==1{$1=$1}1' file

you could paste some example to let us know why your header was not modified.

Upvotes: 1

Related Questions