I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help.
set START = xxxx
set END = yyyy
set selected_file = `awk '/$START/,/$END/' filename`
I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help.
set START = xxxx
set END = yyyy
set selected_file = `awk '/$START/,/$END/' filename`
supposedly, the -v switch is used to passed in shell variables
please check your man page.
try
shellvar1=xxx
shellvar2=zzz
whatever shell stuff | awk '
{
awkvar1="'$shellvar1'" # This is shell var 1
awkvar2="'$shellvar2'" # This is shell var 2
do your awk thing
}'
Note the double and single quotes....
Try This
Gaurav
awk -v ST=$START -v EN=$END '/ST/,/EN/' filename ==> can't work , but thanks anyway
i found the solution ->
set selected_file = `awk '/'"$START"'/,/'"$END"'/' filename`
why its not working? can u paste the output of this command.
also note that if u are on Solaris use nawk instead of awk
it doesn't work because 'ST' and 'EN' are taken as literal strings and not as 'variables':
awk -v ST=$START -v EN=$END '$0 ~ ST , $0 ~ EN' filename
Hi vgersh,
Thanks, your's works. Can you explain to me what does your syntax '$0 ~ ST , $0 ~ EN' mean ?
reminds me of '/ST/,/EN/', doesn't it?
Hi All,
I need to pass variable to awk. But the variable is in shell array format.
How do i pass it thro' ?
my awk script is below where $LOT[$num] is the shell array variable.
$2 ~ "'"$LOT['"$num"']"'" && $3 ~ "SMR1" {gds1 = $6;}
$2 ~ "'"$LOT['"$num"']"'" && $3 ~ "SMR2" {gds2 = $6;}
$2 ~ "'"$LOT['"$num"']"'" && $3 ~ "SMRG" {gdsG = $6;}
END{printf ("%5s\n",gds1 + gds2 + gdsG );}
awk -v num=$num ' {
$2 ~ "'"${LOT[num]}"'" && $3 ~ "SMR1" {gds1 = $6;}
$2 ~ "'"${LOT[num]}"'" && $3 ~ "SMR2" {gds2 = $6;}
$2 ~ "'"${LOT[num]}"'" && $3 ~ "SMRG" {gdsG = $6;}
}
END{printf ("%5s\n",gds1 + gds2 + gdsG );} ' file
hi Anbu,
It didn;t seem to work.
Error is "no match".
I am using solaris and using csh
I used "nawk -v num=$num -f awking2 filename" where awking2 is my awk file.
Can you help ?
Insert the awk script directly in your shell script instead of awk file
Hi anbu,
I still can't get it to work. Error msg is "Unmatched ' "
Can you show the trace of your script?
set num = 1
while ( $num <= $#table )
set check_file = `cat $script/test.csv|grep $LOT[$num]|grep $aa[$num]| awk '{print("already_appended")}'`
echo "$table[$num] is undercheck now."
if ( $check_file != "already_appended" ) then
nawk -f $script_dir/summary/gather_awk $table[$num] >> $script_dir/test.csv
echo "$table[$num] will be appended"
#nawk -v num=$num -f awking2 *csv
nawk -v num=$num ' {
$2 ~ "'"${LOT[num]}"'" && $3 ~ "g1" {gds1 = $6;}
$2 ~ "'"${LOT[num]}"'" && $3 ~ "g2" {gds2 = $6;}
$2 ~ "'"${LOT[num]}"'" && $3 ~ "gG" {gdsG = $6;}
}
END{printf ("%5s\n",gds1 + gds2 + gdsG );} ' *csv
set total = `cat $script/test.csv|grep $LOT[$num]|grep $suffix[$num]|awk '{print($6)}'`
echo ""
else
echo "$table[$num] will NOT be appended"
endif
set num = `echo $num + 1|bc`
end
I am not familiar with the csh syntax.In ksh we use following command to get the trace.
set -vx
Csh don;t use that, i believe.
Anbu , is there any other ways ?
check this link for unmatched error
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=996187&admit=-682735245\+1171606612762\+28353475
check this link for trace in C shell
I tired to use the #/bin/csh -vx and the code paused at the nawk line.
nawk -v num=$num '{
Unmatched '
Is there some thing wrong with the syntax of "'"${LOT[num]}"'" ?