I am trying to pass parameter to the awk command but not getting substituted. Can you please help? I can see the value of $i inside awk is not representing the parameter but uses the file record.
grep NVARCHAR2 test2 | awk '{print substr($4,12)}' > Nvarcharfields
for i in `cat Nvarcharfields`
do
echo $i
awk '{if (substr($2,13)=="\"$i\"") {$10="ChangeX"} print $0}' test2 > test6
done
You iterate over file test2 for every grepped value. Did you consider what happens with duplicates? Cutting off the first 11 characters of a field (even a unique one) can create duplicates.
Any combination of grep, awk, iteration, awk can usually be made into a single awk with the use of arrays. I am particularly suspicious of for i in cat.
It would be helpful to see some data samples. I suspect between substr (12) and substr (13) may lurk a one-off error.
Thanks for your response. This command is working fine when I run through command line but in script the value is not getting substituted. Through debug (sh -x), I can see the below command is running: awk -v 'Item="ZWL2N1"' '{ if (substr($2,14,6) == Item) $3="ChangeX"; print $0}' test2
The single quote in 'item="ZWL2N1"' is making the issue seems.
In this forum backticks mean a low level protection, and are not shown then.
Please wrap your whole code in ``` (markdown) lines; this means a strong protection of the code lines, and single backticks in it are just backticks. (And the tripple backticks markdowns are not shown then.)
BTW backticks in the shell are deprecated, use the POSIX $( ) instead!
If you single-quote the expression after -v, two separate bad things happen:
(a) The single quotes protect the double-quotes, which then become part of the value of the variable itself, so it then cannot match the data as expected.
Conclusion:
(a) Your command in the script is different to the one on the command line.
(b) You are not running the script that you think you are.
(c) You are running a shell that implements set -x badly, by quoting things that are not really there.
Maybe your value being passed with the -v has whitespace or special characters. In that case, Bash single-quotes the whole arg to -v to avoid word-splitting. Because the quotes are removed when the command is parsed, the value passed with -v is a single arg by the time awk sees it.
Please show exactly and completely what you script does do. Being told "it still has quotes" is unhelpful if you don't understand why they might be necessary in some cases.
This is one looong running thread with confusing posts from the OP.
your original script:
grep NVARCHAR2 test2 | awk '{print substr($4,12)}' > Nvarcharfields
for i in `cat Nvarcharfields`
do
echo $i
awk '{if (substr($2,13)=="\"$i\"") {$10="ChangeX"} print $0}' test2 > test6
done
How about this withOUT the loop and all the extra hops you wanted to go with:
#/bin/bash
awk '
FNR==NR {if (/NVARCHAR2/) f1[substr($4,12)];next}
substr($2,13) in f1 {$10="ChangedX"}
1
' test2 test2 > test6
Not tested. Just simplifying the steps, and I don't know WHAT is behind these steps/logic and/or what's the bigger picture you're trying to achieve, but it should get you started with the better skeleton.
It could also help YOU/us, if you could provide a small representative sample of test2.
BTW, PLEASE start using markdown code tags when posting code/data samples - I believe you've been asked/warned before.