Dropping a field 
Author Message
 Dropping a field

Is there a "standard" way of dropping a field, and then having
$0 recomputed and NF descreased?

For example, suppose you wanted to drop $5 and $7 out of
a line where NF=10.  You could then drop $7 first, then $5.

It seems that "delete $7" would be what I'd want, but that only
works for arrays.



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field


Quote:
>Is there a "standard" way of dropping a field, and then having
>$0 recomputed and NF descreased?

>For example, suppose you wanted to drop $5 and $7 out of
>a line where NF=10.  You could then drop $7 first, then $5.

>It seems that "delete $7" would be what I'd want, but that only
>works for arrays.

Or use
 {
 $7=""; $5=""
etc
Quote:
}

awk will (very kindly) recompute NF

Note that all separators between 'words' (ie twixt $1, $2, $3 etc)
will become just one space unless you alter OFS

Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.efiche.com **



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field
I'd tried that:

BEGIN { FS = OFS = "," }

{ $7 = ""; $5 =""; print NF }

on:

1,2,3,4,5,6,7,8,9,0

and the program prints: 10

This makes sense, because it matches the input file
1,2,3,4,,6,,8,9,0

Quote:



>>Is there a "standard" way of dropping a field, and then having
>>$0 recomputed and NF descreased?

>>For example, suppose you wanted to drop $5 and $7 out of
>>a line where NF=10.  You could then drop $7 first, then $5.

>>It seems that "delete $7" would be what I'd want, but that only
>>works for arrays.

>Or use
> {
> $7=""; $5=""
>etc
>}
>awk will (very kindly) recompute NF

>Note that all separators between 'words' (ie twixt $1, $2, $3 etc)
>will become just one space unless you alter OFS

>Mark
>--
>Mark Katz
>ISPC, London - Innovation in data-delivery tools
>Tel: (44) 181-455 4665, Fax (44) 181-458 9554
>** See our website at http://www.efiche.com **



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field


Quote:
>I'd tried that:

>BEGIN { FS = OFS = "," }
>{ $7 = ""; $5 =""; print NF }

>on:

>1,2,3,4,5,6,7,8,9,0

>and the program prints: 10

>This makes sense, because it matches the input file
>1,2,3,4,,6,,8,9,0

Oops - you are dead right ;-)

I used the data line of
1 2 3 4 5 6 7 8 9 10
If however you replace the multiple spaces with a single one
  { $7 = ""; $5 =""; gsub("  ", " ", $0); print NF }
I think it works...

Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.efiche.com **



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field
<snip>
Quote:
>>This makes sense, because it matches the input file
>>1,2,3,4,,6,,8,9,0

>Oops - you are dead right ;-)

>I used the data line of
>1 2 3 4 5 6 7 8 9 10
>If however you replace the multiple spaces with a single one
>  { $7 = ""; $5 =""; gsub("  ", " ", $0); print NF }
>I think it works...

<snip>

If I change the gsub to be gsub(",,", ",", $0) I get the desired
results for this example.  My real data though is rather large,
and has many sets of empty values (for example,
1,2,3,,,,,4,5,6,7,9
So, I can't replace double ,, with a single ,.



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field

Quote:

><snip>
>>>This makes sense, because it matches the input file
>>>1,2,3,4,,6,,8,9,0

>>Oops - you are dead right ;-)

>>I used the data line of
>>1 2 3 4 5 6 7 8 9 10
>>If however you replace the multiple spaces with a single one
>>  { $7 = ""; $5 =""; gsub("  ", " ", $0); print NF }
>>I think it works...

><snip>

>If I change the gsub to be gsub(",,", ",", $0) I get the desired
>results for this example.  My real data though is rather large,
>and has many sets of empty values (for example,
>1,2,3,,,,,4,5,6,7,9
>So, I can't replace double ,, with a single ,.

So, replace "," with ", " first, then remove the fields, then
change ",," to ",", then change ", " to "," then print the line:

gawk 'BEGIN{FS=OFS=","}
      {gsub(/,/,", ");
      $7="";$5="";
      gsub(/,,/,",");
      gsub(/, /,",");print}' infile

for this infile:

1,2,,,5,6,7,8,9,,,,13

it produces this output:

1,2,,,6,8,9,,,,13

Which is what you want, IIRC.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field
Thank.  That's got it.
Quote:

>So, replace "," with ", " first, then remove the fields, then
>change ",," to ",", then change ", " to "," then print the line:

>gawk 'BEGIN{FS=OFS=","}
>      {gsub(/,/,", ");
>      $7="";$5="";
>      gsub(/,,/,",");
>      gsub(/, /,",");print}' infile

>for this infile:

>1,2,,,5,6,7,8,9,,,,13

>it produces this output:

>1,2,,,6,8,9,,,,13

>Which is what you want, IIRC.  :-)



Sun, 04 Nov 2001 03:00:00 GMT  
 Dropping a field

Quote:

>Is there a "standard" way of dropping a field, and then having
>$0 recomputed and NF descreased?

>For example, suppose you wanted to drop $5 and $7 out of
>a line where NF=10.  You could then drop $7 first, then $5.

>It seems that "delete $7" would be what I'd want, but that only
>works for arrays.

Late to the party! I know someone's already given you a solution that works for
your specifications. Here's a generic solution that should work for any FS. It
requires new awk. The basic idea is to create an array of character positions
within $0 which mark the last character in each field. The dropfields()
function recreates $0 (and $1, $2,... and NF) with specified fields AND
immediately preceding field separators deleted.

Possibly extreme overkill, but what the heck.

dropfields.awk
---------------------
BEGIN { droplist[5] = droplist[7] }
{ # for testing
  print
  print dropfields(droplist)
  print "------------"

Quote:
}

function dropfields(droplist,  fs, i, s) {
  if (NF == 0) return $0

  _fp[0] = 0

  if (FS == " ") {
    fs = "^[ \t]+"
    match($0, fs)
    _fp[1] = (RSTART ? RLENGTH : 0) + length($1)
  } else {
    fs = "^" FS
    _fp[1] = length($1)
  }

  for (i = 2; length(s = substr($0, _fp[i - 1] + 1)); ++i) {
    match(s, fs)
    _fp[i] = _fp[i - 1] + RLENGTH + length($i)
  }

  s = ""
  for (i = 1; i <= NF; i++)
    if (!(i in droplist))
      s = s substr($0, _fp[i-1] + 1, _fp[i] - _fp[i-1])

  for (i in _fp) delete _fp[i]

  return $0 = s

Quote:
}

testdata1
--------------
A:B-C;D:E;F:G-H;I:J
A:B,C:D-E-F,G;H:I,J
A:B;C;D-E,F;G,H-I,J
A-B:C;D:E;F:G:H;I;J
A:B-C;D-E;F:G:H;I-J
A:B,C-D-E-F;G,H;I,J
A-B;C-D-E:F;G-H,I-J
A,B-C-D-E:F:G-H-I:J
A,B;C:D,E;F:G-H-I:J
A-B-C:D,E:F-G-H-I;J

gawk -F"[-,;:]" -f dropfields.awk testdata
---------------------------------------------------------
A:B-C;D:E;F:G-H;I:J
A:B-C;D;F-H;I:J
------------
A:B,C:D-E-F,G;H:I,J
A:B,C:D-F;H:I,J
------------
A:B;C;D-E,F;G,H-I,J
A:B;C;D,F,H-I,J
------------
A-B:C;D:E;F:G:H;I;J
A-B:C;D;F:H;I;J
------------
A:B-C;D-E;F:G:H;I-J
A:B-C;D;F:H;I-J
------------
A:B,C-D-E-F;G,H;I,J
A:B,C-D-F,H;I,J
------------
A-B;C-D-E:F;G-H,I-J
A-B;C-D:F-H,I-J
------------
A,B-C-D-E:F:G-H-I:J
A,B-C-D:F-H-I:J
------------
A,B;C:D,E;F:G-H-I:J
A,B;C:D;F-H-I:J
------------
A-B-C:D,E:F-G-H-I;J
A-B-C:D:F-H-I;J
------------

testdata2
--------------
 A  B  C D E  F G  H    I   J
A    B   C    D   E    F  G   H    I J
A   B  C    D   E    F    G    H   I    J
A B    C  D   E  F    G H I J
 A    B  C   D  E  F    G  H    I    J
A   B C   D E    F G   H  I    J
A B   C D  E    F G H  I J
A B   C D E    F    G   H   I   J
 A    B  C   D    E F    G   H  I  J
 A    B C D E  F G   H I    J

gawk -f dropfields.awk testdata2 | tr ' ' '+'
-----------------------------------------------------------
.A..B..C.D.E..F.G..H....I...J
.A..B..C.D..F..H....I...J
------------
A....B...C....D...E....F..G...H....I.J
A....B...C....D....F...H....I.J
------------
A...B..C....D...E....F....G....H...I....J
A...B..C....D....F....H...I....J
------------
A.B....C..D...E..F....G.H.I.J
A.B....C..D..F.H.I.J
------------
.A....B..C...D..E..F....G..H....I....J
.A....B..C...D..F..H....I....J
------------
A...B.C...D.E....F.G...H..I....J
A...B.C...D....F...H..I....J
------------
A.B...C.D..E....F.G.H..I.J
A.B...C.D....F.H..I.J
------------
A.B...C.D.E....F....G...H...I...J
A.B...C.D....F...H...I...J
------------
.A....B..C...D....E.F....G...H..I..J
.A....B..C...D.F...H..I..J
------------
.A....B.C.D.E..F.G...H.I....J
.A....B.C.D..F...H.I....J
------------



Mon, 05 Nov 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Clear File Combo Drop Down Field?

2. Drag&Drop, multi fields from app to app

3. Drop list on EIP field in C4b, ABC

4. Drop Combo - can't get back a to blank field

5. Show 2nd field format in listbox with drop

6. Assign multiple fields from a file drop box?

7. Is a Drop Combo a required field?

8. Default field in a drop down list

9. Field/Procedure Drop Pick List in C55 Editor

10. Drag and Drop - How to get what was dropped onto

11. Drop combo related to another drop combo

12. Drop Combo dependent on another drop combo

 

 
Powered by phpBB® Forum Software