how to do the shell's "shift" in awk 
Author Message
 how to do the shell's "shift" in awk

Hello again awk professionals, I was wondering how to enjoy the ease of the
"shift" statement of the shell in awk?  How would one write
it?  One would think

$1=""; print
$2=""; print
but the field separators are still there and the field numbers didn't
get shifted.

waitasecond fellas, I think I got it:

awk '
function shift() { #should also accept parameter
                   #of how many to shift by, like the shell
  for(i=1;i<=NF;i++){
    $i=$(i+1)}
  NF-- #bye bye field separator too

Quote:
}

{ #testing:
for (m=1;m<=10;m++){
print
shift()
Quote:
}}

' <<! |cat -vte
field1 field2 field3 field4 field5 field6
hello1 hello2 hello3 hello4 hello5 hello6
!
--
http://www.*-*-*.com/ :restore .com.


Wed, 10 Dec 2003 13:35:35 GMT  
 how to do the shell's "shift" in awk

Dan> Hello again awk professionals, I was wondering how to enjoy the ease of the
Dan> "shift" statement of the shell in awk?

I spent 10 minutes to bang this into working shape, but haven't polished it.

for s in  "" 1 2 3 4 5 6 7 8 0 -1  ; do
awk '
function shift(p) { #optional p: how many to shift by, like the shell
  if (p=="")p=1;else if (p<0) exit 33 #should print error message
  for(i=1;i<=NF;i++){
    $i=$(i+p)}
  NF-=(p>NF?NF:p) #bye bye field separator too

Quote:
}

{ #testing:
for (m=1;m<=10;m++){
print
shift('$s')
Quote:
}}

' <<! |cat -vte
field1 field2 field3 field4 field5 field6
hello1 hello2 hello3 hello4 hello5 hello6
!
done
--
http://www.geocities.com/jidanni Tel+886-4-25854780 e-mail:restore .com.


Fri, 19 Dec 2003 11:22:20 GMT  
 how to do the shell's "shift" in awk

Quote:


>Dan> Hello again awk professionals, I was wondering how to enjoy the ease
of the
>Dan> "shift" statement of the shell in awk?

>I spent 10 minutes to bang this into working shape, but haven't polished
it.

>for s in  "" 1 2 3 4 5 6 7 8 0 -1  ; do
>awk '
>function shift(p) { #optional p: how many to shift by, like the shell
>  if (p=="")p=1;else if (p<0) exit 33 #should print error message
>  for(i=1;i<=NF;i++){
>    $i=$(i+p)}
>  NF-=(p>NF?NF:p) #bye bye field separator too
>}
>{ #testing:
>for (m=1;m<=10;m++){
>print
>shift('$s')
>}}
>' <<! |cat -vte
>field1 field2 field3 field4 field5 field6
>hello1 hello2 hello3 hello4 hello5 hello6
>!
>done

All you need to do is delete all characters through the field separator
between the p-th and (p+1)-th fields. Your literal shifting of fields does
too much work - rebuilding $0 on each iteration, replacing actual field
separators (possibly multiple spaces) with OFS. Better just to locate the
(p+1)-th field.

function shift(n  , k, fs) {
  if (n == "") {
    n = 1
  } else if (n < 0) {
    exit 33
  }

  fs = (FS == " " ? "[ \t]+" : FS)

  if (FS == " ") sub(/^[ \t]*/, "")

  while (n-- > 0) {
    if (match(substr($0, k), fs) > 0) {
      k += RSTART + RLENGTH
    } else {
      k += length
      break
    }
  }

  $0 = substr($0, k)

- Show quoted text -

Quote:
}



Sat, 20 Dec 2003 08:04:32 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. 10.20: root can't "su tftp": No shell

2. '"""' and linefeed characters

3. Shift a "modulo" area

4. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

5. "Shelling Out" to DOS

6. How to "shell out"?

7. Writing "Shell Scripts" in SICStus Prolog

8. "expect" shell fails after aix upgrade

9. Wanted: Character-mode "Menu Shell"

10. "&" and Awk

11. awk "search and replace"

12. "Embedding" an awk script in bash

 

 
Powered by phpBB® Forum Software