Unix/Linux Go Back    



change field separator only from nth field until NF

UNIX for Dummies Questions & Answers




Kindly Note - This is a Single User Post by Forum Member beca123456 Regarding:
change field separator only from nth field until NF.
Please Follow The Primary Link Above to View the Full Discussion.

   
Old Unix and Linux 08-17-2012   -   Original Discussion by beca123456
beca123456's Unix or Linux Image
beca123456 beca123456 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 23 April 2018, 9:24 AM EDT
Posts: 113
Thanks: 62
Thanked 1 Time in 1 Post
change field separator only from nth field until NF

Hi !

input:


Code:
111|222|333|aaa|bbb|ccc
999|888|777|nnn|kkk
444|666|555|eee|ttt|ooo|ppp

With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records).

In order to get:


Code:
111|222|333|aaa; bbb; ccc
999|888|777|nnn; kkk
444|666|555|eee; ttt; ooo; ppp

I tried something like:


Code:
gawk 'BEGIN{FS=OFS="|"}{for(i=5; i<=NF; i++) $4 = $4 ($4?"; ":"")$i}1' input

It works for the 4th field, but it also prints the original fields from $5 to $NF after:


Code:
111|222|333|aaa; bbb; ccc|bbb|ccc
999|888|777|nnn; kkk|kkk
444|666|555|eee; ttt; ooo; ppp|ttt|ooo|ppp

Is there any way to to create an array from $4 until $NF without knowing the number of fields (a[$4...$NF])???

Thanks for your help !