Here in the above output, if we observe,the bb value got inserted at 19th field in the input file. But the spaces got removed from the fields which contains a combination of blank space and values.
For ex:- see field2 . field2 = 2.891 but after inserting bb value the leading blank space at field2 got removed. Likewise blankspaces got removed in other fields as well.
with the above code, i can keep the 19th field value as 2.979 and the field width also adjusted properly. But the problem is the original value of 19th field in my input 76.265 is getting removed. Al other field values are coming correctly.
I just need to insert 2.979 in 19th filed but i should not remove any other field values. How can i overcome this ?
Thanks for the reply. I have modified the loop as you suggested. In the output , 2.979 placed in 20th field and 76.265 placed in 19th field .
So i just modified as below:
for (i=2; i<=CNF+2; i++) {printf "%7s", T; if ( i == 18) {printf "%7s", " 2.979"}}
Now the value 2.979 is placed in 19th field and 76.265 in 20th field . and all other fields placed correctly.
Also i have question that , if 2.979 value is stored in one variable, then can i assign it to T[19] ?
Reason for this is, the 2.979 is not constant value, i am taking the value from some other file. So if i pull out the value from the file and store it in one variable then can i assign the variable value to array element ?
I have tried something on this but did not get the proper result.
You want to insert that value, i.e. it will be $19, $19 will become $20, etc.? Then you need to shove every single value one index back. Like for (i=CNF; i>18; i--) $(i+1) = $i .
IF there's NO empty fields in your file, you could extend the 19th field like $19 = 2.979 " " $19 , and then split $0 into T for formatted output.
EDIT: Of course you can use a variable for the value to be inserted: $19 = var " " $19 ! But - it needs to be an awk variable, NOT a shell variable.
You can't import shell variables into awk the way you do in above post. Please read man awk and/or many threads in here about how to use the -v option to pass variables to awk .
In your case, with one single value in file nawk6, that could be done like
Thanks for the reply.I have tried both of your codes and the both worked fine.
Now i am applying the same code on seven input files where as my nawk6 contains 7 lines in it. i can do that but i am failed to take the output to seben different output files.
As long as there is one line in 7lines for each input file, the values in the file 7lines fall in the range -99.999 through 999.999 , the value on one line in 7lines is to added between the 18th and 19th field in each line in the corresponding input file (as long as there is one or more lines in each input file), the length of the 1st field on each line in an input file is a constant width within that input file, and the length of each field (other than the 1st field) in every input file is 7; the following awk script doesn't need you to run an awk script to create the awk6 file, nor run sed scripts.
nawk '
NR == FNR {
f19[NR] = $1
next
}
FNR == 1 {
if(fc++)
close(ofn)
len = length($1) + 17 * 7
ofn = "output" fc
}
{ printf("%s%7.3f%s\n", substr($0, 1, len), f19[fc],
substr($0, len + 1)) > ofn
}' 7lines input[1-7]
awk '
FNR == NR {P[NR] = $0 # collect to be inserted values from first file into P array
next
}
FNR == 1 {FN++ # increment file number with every new file
}
{printf "%s", $1 > ("output" FN) # print first element with its own format to respective output file
$19 = P[FN] FS $19 # insert respective value from P array in front of field 19
$0 = $0 # recompute NF
for (i=2; i<=NF; i++) printf "%7s", $i > ("output" FN)
# print all fields with constant format to output file
printf RS > ("output" FN) # print line terminator
}
' nawk7 file1 file1 file1 file1 file1
nawk ' # Start nawk script.
# The following section of code is processed when we are reading the 1st input
# file (i.e., when the current number of lines read (NR) is the same as the
# current number of lines read from the current input file (FNR)).
NR == FNR {
f19[NR] = $1 # Gather the values that are to be inserted as the new
# field #19 in subsequent files.
next # Read the next input line without processing the
# remaining steps in this script.
}
# The following section of code is processed when we see the 1st line in any
# subsequent file...
FNR == 1 {
# Increment a count of the number of files seen...
if(fc++)
# and, if it is not the first one, close the previous output
# file.
close(ofn)
# Calculate the the number of characters in the current input line that
# appear before the spot where the new field is to be added. It is
# assumed that the length of the first field in each line is a constant
# within any input file (which is true by definition when input files
# contain only one line).
len = length($1) + 17 * 7
# Set the output file name to be used when processing this input file.
ofn = "output" fc
}
# The following section is processed for each line in the current file.
{ # Print the 1st len characters of the current input line, followed by
# the value to be inserted as field #19 (as a 7 character floating
# point value with three digits after the decimal point) in this file,
# followed by the remainder of the current input line to the current
# output file.
printf("%s%7.3f%s\n", substr($0, 1, len), f19[fc],
substr($0, len + 1)) > ofn
}' 7lines input[1-7]
# The above line terminates the nawk script and names the file containing the
# values to be inserted (which much be first) and the names of the input files
# to be modified in the order you want them to be processed.
Let me know if anything needs further explanation.