Let say you got a �~� delimited input file and this file has 6 input fields and now I want to add 12-5=7 number of �~� into this input file in order to make it 12 fields
Welcome to forums, please use code tags for your Input_file/commands/codes as per forum rules.
Could you please try following and let me know if this helps you.
Apart from the fact that in your post#1 the spec ("No.of field = 12") doesn't match the desired result (13 fields) ALL solutions proposed produce an easily adaptable result that should satisfy your request.
Could you please go through following explanation and let me know if this helps you.
awk -F"~" '{ ##Making field separator as ~ here for each line of Input_file.
i=NF; ##creating a variable named i whose value is equal to value of NF, NF is out of box variable which defines number of fields in a line of Input_file.
while(i<14){ ##Initiating while loop here which will run from i value from 0 to 13 here.
val=val?val "~":"~";##creating a variable named val whose value will be having its value to ~ on very first occurrence and will concatenate itself each time with its previous value. Where ? means if condition is TRUE then execute statement followed by it or condition is FALSE then execute statement after :
i++ ##incrementing the value of variable named i here.
};
print $0 val; ##printing current line and value of variable val.
val="" ##nullifying value of val here.
}' Input_file ##Mentioning Input_file here.
Cool! As RudiC pointed out, what you asked for will actually give you thirteen fields. You want to reduce those numbers by 1 to get the twelve you desired (or use RudiC's solution, which gives the desired result already (although this didn't work for me, it might for you - but on Solaris, who knows? :))).
Without showing us what input you had that produced that output, we have to assume that your input had leading vertical bars. When I try the code you showed us in post #16 with the input: