I'm trying to read two text files so I can check if some fields are the same in both files. I can easily extract the fields in command line but something goes wrong when doing from a bash script.
I generate the file (list of files) in a for loop (tried echo and printf commands)
printf "$servidor1$gfs1_dir$gfs1_file\n" >> server1
You can see the output of cat command
cat server1
ftp://server/pub/data/nccf/com/gfs/prod/gfs.2014011400/gfs.t00z.pgrbf00.grib2
ftp://server/pub/data/nccf/com/gfs/prod/gfs.2014011400/gfs.t00z.pgrbf06.grib2
If I try from the command line it runs fine. Two lines/records in the file are shown:
awk 'BEGIN { FS="/"} {print $11}' server1
gfs.t00z.pgrbf00.grib2
gfs.t00z.pgrbf06.grib2
But if I want to set FNR there comes the error (in the script awk it is used to build a variable named fremote)
awk 'BEGIN { FS="/"} { RS="\n"} {FNR == 1} {print $11}' server1
gfs.t00z.pgrbf00.grib2
gfs.t00z.pgrbf06.grib2
The same occurs when I create the fremote var in the bash script (i stands for the loop variable in the script)
i=1
fremote=`awk -v i=$i 'BEGIN { FS="/"} { RS="\n"} {FNR == i} {print $11}' servidor1-file.list`
echo $fremote
gfs.t00z.pgrbf00.grib2 gfs.t00z.pgrbf06.grib2
Maybe it is related with the way server1 file is created, maybe how it is accessed by awk. I can't find the right point.
Thanks in advance for your help. I'll go on working o this issue and post the answer if found.
EDIT
From the comments I add the code in the bash script where awk is invoked (hope it helps to understand what I'm trying). I have two files, list of local files and list of remote files in the server. I try to build two vars flocal and fremote to check if they are the same. Maybe there are easier and smarter ways to check.
while [ $i -le $nlocal ]
do
flocal=`awk -v i=$i 'FNR == i {print $1}' lista.local`
fremote=`awk -v i=$i 'BEGIN { FS="/"} {FNR == $i} {print $11}' $2`
if [ "$flocal" != "$fremote" ]; then
echo "Some file missing" >> $log
flag_check_descarga=0
else
contador=$(($contador + 1))
echo $contador "Download OK" $flocal >> $log
fi
i=$(( $i + 1 ))
done
FS,OFS,RS,ORS...should be set inBEGINblock. btw, why you want to setFNR? and what do you want to get?RSthere? Why are you setting it for each line?==is equality not assignment. If you get an error you should show us the error text. I don't know that setting FNR is going to do what you expect (I don't know what awk is going to do when you change that, possibly nothing).FNR == iwhere i is the loop variable as I want to check every single line.awk -F/'NR==FNR{files[$1]=1} ($11 not in files) {printf "File missing: %s", $11}' lista.local server1or something like that.