You are not logged in.
I am trying to print and copy latest updated packages.
#! /bin/bash
DT=$(cat /var/log/pacman.log | tail -1 | awk -F "[[ ]" '{print $2}')
echo $DT
for i in $(cat /var/log/pacman.log | awk -F "[ )]" -v lastdate=$DT '/lastdate/ && /upgraded/ {print $5"-"$8"*pkg.tar.xz"}'); do
echo $i
cp /var/cache/pacman/pkg/$i ~/updpkg/
done
I set external variable "DT" in awk using "-v" options. But it doesn't work as I expect.
this works as I expect:
#! /bin/bash
# DT=$(cat /var/log/pacman.log | tail -1 | awk -F "[[ ]" '{print $2}')
# echo $DT
for i in $(cat /var/log/pacman.log | awk -F "[ )]" '/2016-09-10/ && /upgraded/ {print $5"-"$8"*pkg.tar.xz"}'); do
echo $i
cp /var/cache/pacman/pkg/$i ~/updpkg/
done
How can I print external variable in awk for pattern matching?
Last edited by duyinthee (2016-09-10 11:07:08)
Offline
ok got it.
what I need is
-v lastdate="$DT" '$0 ~ lastdate && .....
A better method is to use awk’s variable assignment feature (see Assignment Options) to assign the shell variable’s value to an awk variable. Then use dynamic regexps to match the pattern (see Computed Regexps). The following shows how to redo the previous example using this technique:
printf "Enter search pattern: " read pattern awk -v pat="$pattern" '$0 ~ pat { nmatches++ } END { print nmatches, "found" }' /path/to/data
Now, the awk program is just one single-quoted string. The assignment ‘-v pat="$pattern"’ still requires double quotes, in case there is whitespace in the value of $pattern. The awk variable pat could be named pattern too, but that would be more confusing. Using a variable also provides more flexibility, as the variable can be used anywhere inside the program—for printing, as an array subscript, or for any other use—without requiring the quoting tricks at every point in the program.
Offline