You are not logged in.

#1 2011-02-15 17:24:20

ibendiben
Member
Registered: 2007-10-10
Posts: 519

[SOLVED]Can someone explain this awk code?

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

#2 2011-02-15 18:06:11

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Can someone explain this awk code?

What is it supposed to do? Show just the files? I ask because it includes '/var/cache/pacman/pkg' in the list.

Offline

#3 2011-02-15 18:13:27

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

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

#4 2011-02-15 18:15:48

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Can someone explain this awk code?

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.

Offline

#5 2011-02-15 18:19:01

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

karol wrote:
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

#6 2011-02-15 18:23:24

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

I manages to make the code more clear:

pacman -Qlq pacman | awk 'index($0,previousline"/")!=1 {print previousline} 1 {sub(/\/$/,""); previousline=$0}'

Offline

#7 2011-02-15 18:28:37

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Can someone explain this awk code?

[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

#8 2011-02-15 18:34:05

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED]Can someone explain this awk code?

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

#9 2011-02-15 18:41:09

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED]Can someone explain this awk code?

The awk script is bugged, it doesn't print the last line.

It needs an

END {print prev}

Offline

#10 2011-02-15 18:42:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Can someone explain this awk code?

@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

#11 2011-02-15 18:42:52

awkwood
Member
From: .au <=> .ca
Registered: 2009-04-23
Posts: 91

Re: [SOLVED]Can someone explain this awk code?

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

#12 2011-02-15 18:47:43

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

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

#13 2011-02-15 18:50:36

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

you are all faster! great explanations!!

Offline

#14 2011-02-15 19:03:37

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

Procyon wrote:

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

#15 2011-02-15 19:07:58

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

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

#16 2011-02-15 19:21:43

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED]Can someone explain this awk code?

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

#17 2011-02-15 19:29:03

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: [SOLVED]Can someone explain this awk code?

ibendiben wrote:

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

Board footer

Powered by FluxBB