Reputation: 3812
In bash:
1) For a given groupname
of interest, and
2) a list of keys
of interest, for which we want a table of values, for this groupname,
3) read in a set of files, like those in /usr/share/applications
(see simplified example below),
4) and produce a delimited table, with one line per file, and one field for each given key.
We want only the values of the Name and Exec keys, from only [Desktop Entry]
groups, and from one or more files, like these:
[Desktop Entry]
Name=Root
Comment=Opens
Exec=e2
..
[Desktop Entry]
Comment=Close
Name=Root2
Two lines, one per input file, each in a delimited <Name>,<Exec>
format, ready for import into a database:
Root,e2
Root2,
Each input file is:
[Forgive me if I am asking for a solution to an old problem, but I can't seem to find a good, quick bash way, to do this. Yes, I could code it up with some while and read loops, etc... but surely it's been done before.]
Similar to this Q but more general answer wanted.
Upvotes: 0
Views: 232
Reputation: 22042
If awk
is your option, would you please try the following:
awk -v RS="[" -v FS="\n" '{ # split the file into records on "["
# and split the record into fields on "\n"
name = ""; exec = "" # reset variables
if ($1 == "Desktop Entry]") {
# if the groupname matches
for (i=2; i<=NF; i++) { # loop over the fields (lines) of "key=value" pairs
if (sub(/^Name=/, "", $i)) name = $i
# the field (line) starts with "Name="
else if (sub(/^Exec=/, "", $i)) exec = $i
# the field (line) starts with "Exec="
}
print name "," exec
}
}' file
You can feed multiple files as file1 file2 file3
, dir/file*
or whatever.
Upvotes: 2