Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #1098 > unrolled thread

Linux utility with reverse index facility?

Started byno.top.post@gmail.com
First post2011-05-16 10:38 +0000
Last post2011-05-19 16:15 -0700
Articles 13 — 9 participants

Back to article view | Back to comp.os.linux.misc


Contents

  Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 10:38 +0000
    Re: Linux utility with reverse index facility? pk <pk@pk.invalid> - 2011-05-16 11:41 +0100
      Re: Linux utility with reverse index facility? Ed Morton <mortonspam@gmail.com> - 2011-05-16 07:08 -0500
        Re (2): Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 19:30 +0000
          Re: Re (2): Linux utility with reverse index facility? Tim Watts <tw@dionic.net> - 2011-05-16 22:32 +0100
    Re: Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 07:59 -0400
      Re (2): Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 19:30 +0000
        Re: Re (2): Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 15:59 -0400
    Re: Linux utility with reverse index facility? David Brown <david@westcontrol.removethisbit.com> - 2011-05-16 14:38 +0200
    Re: Linux utility with reverse index facility? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-16 15:08 +0200
    Re: Linux utility with reverse index facility? Robert Nichols <SEE_SIGNATURE@localhost.localdomain.invalid> - 2011-05-17 22:40 -0500
      Re: Linux utility with reverse index facility? Luuk <Luuk@invalid.lan> - 2011-05-18 19:14 +0200
      Re: Linux utility with reverse index facility? Ed Morton <mortonspam@gmail.com> - 2011-05-19 16:15 -0700

#1098 — Linux utility with reverse index facility?

Fromno.top.post@gmail.com
Date2011-05-16 10:38 +0000
SubjectLinux utility with reverse index facility?
Message-ID<iqquq7$i46$1@dont-email.me>
awk &stuff can "give me the the Nth element",
but, without writing your own search-loop,
what can "give me the index of the 'element'
which is <elementValue>" ?

I think it's called 'reverse indexing' ?

== TIA.

[toc] | [next] | [standalone]


#1099

Frompk <pk@pk.invalid>
Date2011-05-16 11:41 +0100
Message-ID<iqqv0v$4uf$1@speranza.aioe.org>
In reply to#1098
no.top.post@gmail.com wrote:

> awk &stuff can "give me the the Nth element",

No it can't.

> but, without writing your own search-loop,
> what can "give me the index of the 'element'
> which is <elementValue>" ?

What does that mean?

[toc] | [prev] | [next] | [standalone]


#1102

FromEd Morton <mortonspam@gmail.com>
Date2011-05-16 07:08 -0500
Message-ID<iqr44b$oug$1@dont-email.me>
In reply to#1099
On 5/16/2011 5:41 AM, pk wrote:
> no.top.post@gmail.com wrote:
>
>> awk&stuff can "give me the the Nth element",
>
> No it can't.

I think what the OP means is that you can do this:

$ echo "a b c" | awk -v f=2 '{print $f}'
b
$ echo "a b c" | awk -v v="b" '{print foo(v)}'
>
>> but, without writing your own search-loop,
>> what can "give me the index of the 'element'
>> which is<elementValue>" ?
>
> What does that mean?
>

I think what the OP means is that he'd wants to be able to do this:

$ echo "a b c" | awk -v v="b" '{print foo(v)}'
2

and is asking if "foo()" exists. No, it doesn't exist in awk. You can do this:

$ echo "a b c" | awk -v v="b" '{print index($0,v)}'
3

to get the starting character position of the string contained in "v" but I 
don't think that's what he wants. He'll just have to suffer through writing:

$ echo "a b c" | awk -v v="b" '{for (f=1;f<=NF;f++) if ($f==v) print f}'
2

or write his own foo() if he needs to do that multiple times in a script:

$ echo "a b c" | awk -v v="b" '{print foo(v)}
function foo(p) {for (f=1;f<=NF;f++) if ($f==p) return f}'
2

Regards,

	Ed.

[toc] | [prev] | [next] | [standalone]


#1113 — Re (2): Linux utility with reverse index facility?

Fromno.top.post@gmail.com
Date2011-05-16 19:30 +0000
SubjectRe (2): Linux utility with reverse index facility?
Message-ID<iqru0q$593$1@dont-email.me>
In reply to#1102
In article <iqr44b$oug$1@dont-email.me>, Ed Morton <mortonspam@gmail.com> wrote: 

> On 5/16/2011 5:41 AM, pk wrote:
> > no.top.post@gmail.com wrote:
> >
> >> awk&stuff can "give me the the Nth element",
> >
> > No it can't.
> 
> I think what the OP means is that you can do this:
> 
> $ echo "a b c" | awk -v f=2 '{print $f}'
> b
> $ echo "a b c" | awk -v v="b" '{print foo(v)}'
> >
> >> but, without writing your own search-loop,
> >> what can "give me the index of the 'element'
> >> which is<elementValue>" ?
> >
> > What does that mean?
> >
> 
> I think what the OP means is that he'd wants to 
> be able to do this:
> 
> $ echo "a b c" | awk -v v="b" '{print foo(v)}'
> 2
Yes!
> and is asking if "foo()" exists. No, it doesn't exist in awk. 
So then your syntax is imaginary, and yet I must understand
it, yet ya'll can't understand my plain english.

>You can do this:
> 
> $ echo "a b c" | awk -v v="b" '{print index($0,v)}'
> 3
That's amazing; and the [first] space is at index 2 !!
> 
> to get the starting character position of the string contained in "v" but I 
> don't think that's what he wants. 
What I want is the KNOWLEDGE, whether *nix designers included
<reverse indexing> which one would use eg. to find:
"which token/field is 'mat' in the record:
"the cat sat on the mat".

> He'll just have to suffer through writing:
> 
> $ echo "a b c" | awk -v v="b" '{for (f=1;f<=NF;f++) if ($f==v) print f}'
> 2
> 
> or write his own foo() if he needs to do that multiple times in a script:
> 
> $ echo "a b c" | awk -v v="b" '{print foo(v)}
> function foo(p) {for (f=1;f<=NF;f++) if ($f==p) return f}'
> 2
OK, so I'd have to do the looping-search-count.

But related:
 since awk uses $1 internally, how do I pass arguments to an
 awk-script. I want to pass 3 strings. like "Class:", "TLC", "JK80K"
 This shows me the fileId & line containing "Class" & "TLC".
 But I want to be able to parameterise "Class" & "TLC".
 --> #!/bin/bash
awk '/Class:/&&/TLC/{print FILENAME ":" $0}'\
 `find /mnt/p11/PhysicalSpace/DirTest  -type f`
 
 == TIA.
 
 > 
> Regards,
> 
> 	Ed.

[toc] | [prev] | [next] | [standalone]


#1116 — Re: Re (2): Linux utility with reverse index facility?

FromTim Watts <tw@dionic.net>
Date2011-05-16 22:32 +0100
SubjectRe: Re (2): Linux utility with reverse index facility?
Message-ID<6ph8a8-blt.ln1@squidward.dionic.net>
In reply to#1113
no.top.post@gmail.com wrote:

> What I want is the KNOWLEDGE, whether *nix designers included
> <reverse indexing> which one would use eg. to find:
> "which token/field is 'mat' in the record:
> "the cat sat on the mat".

Well, if I'm understanding this, then in perl:

#!/usr/bin/perl
$a="the cat sat on the mat"; # Input
@b=split /\s+/, $a;          # space(s) as delimeter, make array of tokens
%c=map {$_ => $i++} @b;     # Make an asociative array (tokenname => index)
print $c{'mat'}'             # try one

index is 0 based, ie 0 is the 1st token

Of course - there is a flaw in that program - hint: what happens if you 
search for the token "the" <- You didn't define what you wanted: a list of 
indices; the first one; the last one?

if the above is too long, you can abbreviate it:

perl -e '$a="the cat sat on the mat";$b={map {$_ => $i++} split /\s+/, $a}-
>{mat};print $b'

(That's one line)


This is why I use perl. Don't ask me about awk, sed, bash - I've found it 
more productive to learn one tool that ha enough functionality to do pretty 
much everything in one place - OK, not so clever on embedded systems where 
perl may not be present, but there you go...

One thing to note: the first part of split is a regex so you can get 
arbitrarily fancy on how you tokenise the string. an Unicode - no problem...

Cheers

Tim

-- 
Tim Watts

[toc] | [prev] | [next] | [standalone]


#1101

From"Chris F.A. Johnson" <cfajohnson@gmail.com>
Date2011-05-16 07:59 -0400
Message-ID<r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>
In reply to#1098
On 2011-05-16, no.top.post@gmail.com wrote:
> awk &stuff can "give me the the Nth element",
> but, without writing your own search-loop,
> what can "give me the index of the 'element'
> which is <elementValue>" ?
>
> I think it's called 'reverse indexing' ?

   Do you want 'grep -n'?

   If not, please be more specific.

-- 
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

[toc] | [prev] | [next] | [standalone]


#1114 — Re (2): Linux utility with reverse index facility?

Fromno.top.post@gmail.com
Date2011-05-16 19:30 +0000
SubjectRe (2): Linux utility with reverse index facility?
Message-ID<iqru16$5cq$1@dont-email.me>
In reply to#1101
In article <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote: 

> On 2011-05-16, no.top.post@gmail.com wrote:
> > awk &stuff can "give me the the Nth element",
> > but, without writing your own search-loop,
> > what can "give me the index of the 'element'
> > which is <elementValue>" ?
> >
> > I think it's called 'reverse indexing' ?
> 
>    Do you want 'grep -n'?
> 
>    If not, please be more specific.

awk & grep are not typically used on arrays of single chars,
but that's the simplest, to ilustrate the concept.
Given the 6char array: [abcdef]
<elementValue> means the value of the element.
The first element has value "a".
"the  index of the 'element' which is "e" is
<the 5th element>; which index is  4 or 5 depending
on whether you count from 0 or 1 respectively.

So what did I mean by "writing your own search-loop"?

AFAIK ruby has <assocoation <memory-structures>>.
That mean ruby has structures which operate like association
memories; so you can ask "what's the index of the element
which has value "dog", in the 'structure' of strings.

== Chris Glur.

[toc] | [prev] | [next] | [standalone]


#1115 — Re: Re (2): Linux utility with reverse index facility?

From"Chris F.A. Johnson" <cfajohnson@gmail.com>
Date2011-05-16 15:59 -0400
SubjectRe: Re (2): Linux utility with reverse index facility?
Message-ID<a9c8a8-3hh.ln1@206-248-139-163.dsl.teksavvy.com>
In reply to#1114
On 2011-05-16, no.top.post@gmail.com wrote:
> In article <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote: 
>
>> On 2011-05-16, no.top.post@gmail.com wrote:
>> > awk &stuff can "give me the the Nth element",
>> > but, without writing your own search-loop,
>> > what can "give me the index of the 'element'
>> > which is <elementValue>" ?
>> >
>> > I think it's called 'reverse indexing' ?
>> 
>>    Do you want 'grep -n'?
>> 
>>    If not, please be more specific.
>
> awk & grep are not typically used on arrays of single chars,
> but that's the simplest, to ilustrate the concept.
> Given the 6char array: [abcdef]

    That's not an array; it's a string (this is shell, not C).

><elementValue> means the value of the element.
> The first element has value "a".

   Why not use the correct term, 'character'? Then we would have
   understood what you meant.

> "the  index of the 'element' which is "e" is
><the 5th element>; which index is  4 or 5 depending
> on whether you count from 0 or 1 respectively.

index() {
    case $1 in
        *"$2"*) idx=${1%%$2*}
            echo "$(( ${#idx} + 1 ))" ;;
        *) echo 0 ;;
    esac
}

$ index abcdef c
3

-- 
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

[toc] | [prev] | [next] | [standalone]


#1103

FromDavid Brown <david@westcontrol.removethisbit.com>
Date2011-05-16 14:38 +0200
Message-ID<T5mdnUihkr4nh0zQnZ2dnUVZ8k2dnZ2d@lyse.net>
In reply to#1098
On 16/05/2011 12:38, no.top.post@gmail.com wrote:
> awk&stuff can "give me the the Nth element",
> but, without writing your own search-loop,
> what can "give me the index of the 'element'
> which is<elementValue>" ?
>
> I think it's called 'reverse indexing' ?
>
> == TIA.
>

Why not write your own loop?  Use your favourite scripting language. 
You haven't described your problem very well, but my guess it is should 
be easy to do in a perl or python script (read in the file, put the data 
through lists, hashes/dicts, sorts, etc., as needed).

I don't know enough about awk or ruby to say if they can handle it easily.

[toc] | [prev] | [next] | [standalone]


#1104

FromJanis Papanagnou <janis_papanagnou@hotmail.com>
Date2011-05-16 15:08 +0200
Message-ID<iqr7k4$r6t$1@speranza.aioe.org>
In reply to#1098
Am 16.05.2011 12:38, schrieb no.top.post@gmail.com:
> awk&stuff can "give me the the Nth element",
> but, without writing your own search-loop,
> what can "give me the index of the 'element'
> which is<elementValue>" ?

You are not very clear about your actual data format.

If you have your data one per line and index is the lineno

   # NR'th element
   awk 'NR == requestedLineNumber' inputfile

   # reverse (NR of requested content, match)
   awk '/requestedContent/ { print NR }' inputfile

   # reverse (NR of requested content, equals)
   awk '$0 == "requestedContent" { print NR }' inputfile

If you have your data in an array you can also use awk to do
the reverse index lookup. (Waiting for your clarifications
about your actual case.)

Janis

>
> I think it's called 'reverse indexing' ?
>
> == TIA.
>

[set f'up-to comp.lang.awk]

[toc] | [prev] | [next] | [standalone]


#1145

FromRobert Nichols <SEE_SIGNATURE@localhost.localdomain.invalid>
Date2011-05-17 22:40 -0500
Message-ID<iqvf42$l3c$1@omega-3a.local>
In reply to#1098
On 05/16/2011 05:38 AM, no.top.post@gmail.com wrote:
> awk&stuff can "give me the the Nth element",
> but, without writing your own search-loop,
> what can "give me the index of the 'element'
> which is<elementValue>" ?
>
> I think it's called 'reverse indexing' ?

In awk, it's just as easy to build an array where your "elementValues"
are the indices and the values stored in the array are ordinal numbers.
I,e., instead of
     myArray[n++] = "cheese"
have
     myArray["cheese"] = n++

Now you can use myArray["something"] in an expression and get back
the number that was stored for that string (or a null string equivalent
to arithmetic 0 if that index was never stored).

-- 
Bob Nichols         AT comcast.net I am "RNichols42"

[toc] | [prev] | [next] | [standalone]


#1160

FromLuuk <Luuk@invalid.lan>
Date2011-05-18 19:14 +0200
Message-ID<4dd3fe8e$0$49048$e4fe514c@news.xs4all.nl>
In reply to#1145
On 18-05-2011 05:40, Robert Nichols wrote:
> On 05/16/2011 05:38 AM, no.top.post@gmail.com wrote:
>> awk&stuff can "give me the the Nth element",
>> but, without writing your own search-loop,
>> what can "give me the index of the 'element'
>> which is<elementValue>" ?
>>
>> I think it's called 'reverse indexing' ?
> 
> In awk, it's just as easy to build an array where your "elementValues"
> are the indices and the values stored in the array are ordinal numbers.
> I,e., instead of
>     myArray[n++] = "cheese"
> have
>     myArray["cheese"] = n++
> 
> Now you can use myArray["something"] in an expression and get back
> the number that was stored for that string (or a null string equivalent
> to arithmetic 0 if that index was never stored).
> 

echo "abcde c"| awk '{ for (x=0;x<length($1);x++) {
    a[substr($1,x,1)]=x;}; print a[$2]; }'

-- 
Luuk

[toc] | [prev] | [next] | [standalone]


#1179

FromEd Morton <mortonspam@gmail.com>
Date2011-05-19 16:15 -0700
Message-ID<65f8c519-cf2c-4ce8-abdd-1b35c24abe7d@c41g2000yqm.googlegroups.com>
In reply to#1145
On May 17, 10:40 pm, Robert Nichols
<SEE_SIGNAT...@localhost.localdomain.invalid> wrote:
> On 05/16/2011 05:38 AM, no.top.p...@gmail.com wrote:
>
> > awk&stuff can "give me the the Nth element",
> > but, without writing your own search-loop,
> > what can "give me the index of the 'element'
> > which is<elementValue>" ?
>
> > I think it's called 'reverse indexing' ?
>
> In awk, it's just as easy to build an array where your "elementValues"
> are the indices and the values stored in the array are ordinal numbers.
> I,e., instead of
>      myArray[n++] = "cheese"
> have
>      myArray["cheese"] = n++
>
> Now you can use myArray["something"] in an expression and get back
> the number that was stored for that string (or a null string equivalent
> to arithmetic 0 if that index was never stored).
>
> --
> Bob Nichols         AT comcast.net I am "RNichols42"

not really robust though. consider what would happen if the same
string appeared twice in a record.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.os.linux.misc


csiph-web