I have one input file which is delimited by pipe. I want to put decimal points in this input file at particular position in particular column and also get the negative sign (if any) at start of that column.
Could you please go through following and let me know if this is helpful for you.
awk 'FNR==NR{ #### FNR==NR condition will be TRUE when first file named decimal.csv is being read.
A[++i]=$0; #### When above condition is TRUE then create an array named A with index of variable i whose index is increasing every occurance whenever above condition is TRUE, so means till first file is being read completly.
next #### skipping all other statements now by using next keyword.
}
{
for(j=1;j<=i;j++){ #### starting for loop now which will start only when second file will be read and wen first file will be completly read then we will get a final valur of variable i so running the loop from j=1 to till i's value.
split(A[j], array,","); #### splitting the value of array A whose delimiter is comma(,). This array is having the values of decimal.csv files, which by splitting into array I wil use it later in code.
if(length($array[1])==array[2]){ #### Now I am comparing the value of field $array[1] which is field mentioned in decimal.csv and value is in file Input_file with length of field provided in decimal.csv file if they are equal then do following actions.
$array[1]=substr($array[1],1,array[3]) "." substr($array[1],array[3]+1); #### Here I am RE-FORMING the field let's say 2nd and 5th(as per your decimal.csv) file by using substr utility of awk. which works on substr(LINE,starting point, ending point) etc.
}
else{ #### In case length of field is NOT equal to length provided in decimal.csv file then do following actions.
q="-" substr($array[1],1,array[2]);$array[1]=substr(q,1,array[3]+1) "." substr(q,array[3]+2);}}; #### creating a variable named q whose value I am constructing by field values by file Input_file, more important placing dot(.) here as per requirement in between field's values.
print #### finally printing the line, not here it will print the new formatted values of fields which we have done in above steps.
}
' decimal.csv FS="|" OFS="|" Input_file #### mentioning first file named decimal.csv then Field separator as | and Output field separator as | for Input_file here.