You are not logged in.
pacman -Qlq pacman | awk 'index($0,prev"/")!=1 && NR!=1 {print prev} 1 {sub(/\/$/,""); prev=$0}'
The results are perfect but I don't quite understand the awk code... very interested to learn the different steps.
Is it possible to simplify?
I would love to see a equivalent using sed, anyone?
Last edited by ibendiben (2011-02-15 19:18:21)
Offline
What is it supposed to do? Show just the files? I ask because it includes '/var/cache/pacman/pkg' in the list.
Offline
Only directories would be better, /var/cache/pacman/pkg is a folder.
Pacman -Qlq also lists:
/var/
/var/cache/
/var/cache/pacman
Goal is to strip the prefixes of all parent directories.
Last edited by ibendiben (2011-02-15 18:17:47)
Offline
Only directories would be better, /var/cache/pacman/pkg is a folder
I think you mean 'files' not 'directories'. That's why I thought I point it out.
Unfortunately I don't understand this awk code myself, so I can't help you.
Offline
ibendiben wrote:Only directories would be better, /var/cache/pacman/pkg is a folder
I think you mean 'files' not 'directories'. That's why I thought I point it out.
Unfortunately I don't understand this awk code myself, so I can't help you.
I'm aware of that! thanks for your quick responses!
Offline
I manages to make the code more clear:
pacman -Qlq pacman | awk 'index($0,previousline"/")!=1 {print previousline} 1 {sub(/\/$/,""); previousline=$0}'
Offline
[karol@black ~]$ pacman -Qlq pacman | awk 'index($0,prev"/")!=1 && NR!=1 {print prev} 1 {sub(/\/$/,""); prev=$0}' > 01
[karol@black ~]$ pacman -Qlq pacman | grep -v /$ > 02
[karol@black ~]$ LC_ALL=C TZ=GMT0 diff -Naur 01 02
--- 01 2011-02-15 18:30:16.000000000 +0000
+++ 02 2011-02-15 18:30:20.000000000 +0000
@@ -73,4 +73,3 @@
/usr/share/pacman/PKGBUILD.proto
/usr/share/pacman/proto.install
/usr/share/zsh/site-functions/_pacman
-/var/cache/pacman/pkg
This means 'pacman -Qlq pacman | grep -v /$' is better and simpler.
Am I missing something?
Last edited by karol (2011-02-15 18:29:16)
Offline
It's a bit easier to understand if you break it up with whitespace and rearrange a little bit of logic
#!/bin/bash
pacman -Qlq pacman | awk '
! index($0, previousline"/") {
print previousline
}
{
sub(/\/$/,"")
previousline=$0
}
'
First stanza: If the content of the var 'previousline' (with a trailing / appended) is not found in the current line, then print the previous line.
Second stanza: For every line, remove the trailing slash and assign it to the var 'previousline'/
karol: your method ignores empty directories that are created as part of the package. The idea is that things like:
/var
/var/cache
/var/cache/pacman
/var/cache/pacman/pkg
...are condense into a single entry: /var/cache/pacman/pkg
Last edited by falconindy (2011-02-15 18:38:04)
Offline
The awk script is bugged, it doesn't print the last line.
It needs an
END {print prev}
Offline
@falconindy
https://bbs.archlinux.org/viewtopic.php … 25#p892525
I wanted to keep only the files, as I thought that's what OP wanted, but if he wants empty dirs too, the awk way is correct.
Offline
Falconindy has it spot on.
Awk delays printing each line until it has read the next one and only prints the previous line if it's not a prefix.
The only bit not explained yet is the "NR!=1", which simply skips the first line (which is empty).
Offline
Think I get the index function.
previousline is actually (current)line? index looks if line after currentline contains the currentline+/
If not, it prints the current line, else ....
Offline
you are all faster! great explanations!!
Offline
The awk script is bugged, it doesn't print the last line.
It needs an
END {print prev}
Thats right!! Thanks a bunch! But I can't get your code to print the missing last line...
Offline
I solved it with:
{ pacman -Qlq pacman; echo; } | awk '
! index($0, previousline"/") {
print previousline
}
{
sub(/\/$/,"")
previousline=$0
}
'
I also found another solution:
pacman -Qlq pacman | awk '$0 !~ last {print last} {last=$0} END {print last}'
Last edited by ibendiben (2011-02-15 19:21:37)
Offline
procyon was being a bit terse. Your method certainly works (and is shorter on keystrokes), but he meant:
pacman -Qlq pacman | awk '
! index($0, previousline"/") {
print previousline
}
{
sub(/\/$/,"")
previousline=$0
}
END {
print previousline
}
'
Offline
I also found another solution:
pacman -Qlq pacman | awk '$0 !~ last {print last} {last=$0} END {print last}'
How does this solution compare to the other one, it looks way simpler, but is there a down side?
Offline