In my Packages file i have multiple packages. I'm able to check the file if a string is inside, and if so, i would like to get the version of the file.
Package: depictiontest
Version: 1.0
Filename: ./debs/com.icr8zy.depictiontest.deb
Size: 810
Description: Do not install. Testing Depiction.
Name: Depiction Test
so the above is part of the many similar looking info of a package. Each time i detected if the package exists i would like to get the Version. is there any possible way?
btw, this is what i use to get check if the file exists.
if grep -q "$filename" /location/Packages; then
#file exists
#get file version <-- stuck here
else
#file does not exists
fi
EDIT: Sorry but maybe i wasn't clear in explaining myself, I would already have the Name of the package and would like to extract the Version of that package only. I do not need a loop to get all the Names and Versions. Hope this clears it... :)
How do you extract the file name in the first place? Why not parse the whole file, then filter out nonexistent file names.
awk '/^Package:/{p=$2}
/^Version:/{v=$2}
/^Filename:/{f=$2}
/^$/{print p, v, f}' Packages |
while read p v f; do
test -e "$f" || continue
echo "$p $v"
done
This is not robust with e.g. file names with spaces, but Packages files don't have file names with spaces anyway. (Your example filename is nonstandard, though; let's assume it's no worse than this.)
You want to make sure there's an empty line at the end of Packages, or force it with { sed '$/^$/d' Packages; echo; } | awk ...
Edit: This assumes a fairly well-formed Packages
file, with an empty line between records. If a record lacks one of these fields, the output will repeat the value from the previous record - that's nasty. If there are multiple adjacent empty lines, it will output the same package twice. Etc. If you want robust parsing, I'd switch to Perl or Python, or use a standard Debian tool (I'm sure there must be one).