awk-equivalent to e.g. cut -f5- 
Author Message
 awk-equivalent to e.g. cut -f5-

Hi!

I need to process my groupinfo-file. For this i'm looking for a way to adress
the fields $1 $5 ... till the end of the line in each record ($1 is the name
of the newsgroup, the description starts at $5). The number of fields per record
is not constant.
Is there a simple way to do this? The only way i know by now is a loop, and
i don't like it.

Gerd.

--
Hi! I'm a signature virus! Copy me into your ~/.signature to help me spread!



Sat, 10 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-
Try:

awk '{print $1 substr($0,<start>)}' in_file

where <start> is the first column of $5.



Quote:
> Hi!

> I need to process my groupinfo-file. For this i'm looking for a way to adress
> the fields $1 $5 ... till the end of the line in each record ($1 is the name
> of the newsgroup, the description starts at $5). The number of fields per record
> is not constant.
> Is there a simple way to do this? The only way i know by now is a loop, and
> i don't like it.

> Gerd.

> --
> Hi! I'm a signature virus! Copy me into your ~/.signature to help me spread!

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 10 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

>> I need to process my groupinfo-file. For this i'm looking for a way to adress
>> the fields $1 $5 ... till the end of the line in each record ($1 is the name
>> of the newsgroup, the description starts at $5). The number of fields per record
>> is not constant.
>> Is there a simple way to do this? The only way i know by now is a loop, and
>> i don't like it.

>Try:

>awk '{print $1 substr($0,<start>)}' in_file

>where <start> is the first column of $5.

Hmm, thanks, it works. But as the fieldlenghts aren't constant, this means

awk '{print $1 substr($0,index($0,$5))}' /var/spool/news/leaf.node/groupinfo

which uses 2 functions concatetenated.
Isn't there any simpler solution? (I like  "cut -f1,5-" :-)

Gerd.

--
Hi! I'm a signature virus! Copy me into your ~/.signature to help me spread!



Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

> [snip]

> Hmm, thanks, it works. But as the fieldlenghts aren't constant, this means

> awk '{print $1 substr($0,index($0,$5))}' /var/spool/news/leaf.node/groupinfo

> which uses 2 functions concatetenated.

Have you considered what happens if what's in $5 occurs somewhere
left of $5?

Quote:
> Isn't there any simpler solution? (I like  "cut -f1,5-" :-)

Just curious: Why can't you use "cut -f1,5-"?

--
Jim Monty

Tempe, Arizona USA



Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-
Am 23.02.2000 schrieb Jim Monty in comp.lang.awk:

Quote:

>> [snip]

>> Hmm, thanks, it works. But as the fieldlenghts aren't constant, this means

>> awk '{print $1 substr($0,index($0,$5))}' /var/spool/news/leaf.node/groupinfo

>> which uses 2 functions concatetenated.

>Have you considered what happens if what's in $5 occurs somewhere
>left of $5?

No. But in my special case, this isn't possible. In general it surely is a
problem, so this is not _THE_ solution.

Quote:
>> Isn't there any simpler solution? (I like  "cut -f1,5-" :-)

>Just curious: Why can't you use "cut -f1,5-"?

I'm using awk to create a .jnewsrc.dsc from the groupinfo-file:
"comp.lang.awk 288 2 0 The AWK programming language." must become
"comp.lang.awk:The AWK programming language."
I think this  ^  colon makes the only use of cut impossible.
cut -f1,5- only produces "comp.lang.awk The AWK programming language."

So my solution now is {print $1 ":" substr($0,index($0,$5))}, but i'm a
bit disappointed that there seems to be no easy way to adress all fields
from a certain starting-point (like here: $5) up to the end of the record.

Gerd.
--
Hi! I'm a signature virus! Copy me into your ~/.signature to help me spread!



Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-
OK. Try:

awk '{printf("%s",$1); for(i=5;i<=NF;++i) printf("%s",$i); print ""}' in_file

I hope this is _THE_ solution and not the loop you dont like.



Quote:

> >> I need to process my groupinfo-file. For this i'm looking for a way to adress
> >> the fields $1 $5 ... till the end of the line in each record ($1 is the name
> >> of the newsgroup, the description starts at $5). The number of fields per record
> >> is not constant.
> >> Is there a simple way to do this? The only way i know by now is a loop, and
> >> i don't like it.

> >Try:

> >awk '{print $1 substr($0,<start>)}' in_file

> >where <start> is the first column of $5.

> Hmm, thanks, it works. But as the fieldlenghts aren't constant, this means

> awk '{print $1 substr($0,index($0,$5))}' /var/spool/news/leaf.node/groupinfo

> which uses 2 functions concatetenated.
> Isn't there any simpler solution? (I like  "cut -f1,5-" :-)

> Gerd.

> --
> Hi! I'm a signature virus! Copy me into your ~/.signature to help me spread!

Sent via Deja.com http://www.deja.com/
Before you buy.


Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

> [snip]

> I'm using awk to create a .jnewsrc.dsc from the groupinfo-file:
> "comp.lang.awk 288 2 0 The AWK programming language." must become
> "comp.lang.awk:The AWK programming language."
> I think this  ^  colon makes the only use of cut impossible.
> cut -f1,5- only produces "comp.lang.awk The AWK programming language."

> So my solution now is {print $1 ":" substr($0,index($0,$5))}, but i'm a
> bit disappointed that there seems to be no easy way to adress all fields
> from a certain starting-point (like here: $5) up to the end of the record.

Use sed:

$ echo 'comp.lang.awk 288 2 0 The AWK Programming Language' |

Quote:
> sed 's/^\([^ ][^ ]*\)  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *\(.*\)/\1:\2/'

comp.lang.awk:The AWK Programming Language
$

Or, if you cannot abide the inelegance of sed's basic regular
expressions, use Perl:

$ echo 'comp.lang.awk 288 2 0 The AWK Programming Language' |

Quote:
> perl -wpe 's/^(\S+)\s+\S+\s+\S+\s+\S+\s+(.*)/$1:$2/'

comp.lang.awk:The AWK Programming Language
$

Naturally, you must tailor the regular expression pattern to your
specific needs.

--
Jim Monty

Tempe, Arizona USA



Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

>OK. Try:

>awk '{printf("%s",$1); for(i=5;i<=NF;++i) printf("%s",$i); print ""}' in_file

Unfortunately, that reparses the line, so multiple spaces get
compressed.

The way to avoid this is to use sub() to change the line.

Here's an example:

awk '{a=$0;

      sub(/[    ][      ]*[^    ][^     ]*/,"",a) #removes what was $2
      sub(/[    ][      ]*[^    ][^     ]*/,"",a) #removes what was $3
      sub(/[    ][      ]*[^    ][^     ]*/,"",a) #removes what was $4
      print a}' infile

The use of a instead of $0 is probably unnecessary

Chuck Demas
Needham, Mass.

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

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



Sun, 11 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-
-----BEGIN PGP SIGNED MESSAGE-----



Quote:
>Use sed:

>$ echo 'comp.lang.awk 288 2 0 The AWK Programming Language' |
>> sed 's/^\([^ ][^ ]*\)  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *\(.*\)/\1:\2/'
>comp.lang.awk:The AWK Programming Language
>$

I don't think I quite understand your rationale behind using
the ``\( \) patterns.''  Why not simply:

: hurricane 2465; echo 'comp.lang.awk 288 2 0 The AWK Programming Language' |
sed -e 's/  *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* */:/'
comp.lang.awk:The AWK Programming Language
: hurricane 2466;

Or, if you want to use tabs as a possible whitespace character in
addition to spaces, the following works (provided I didn't mess any-
thing up in cutting and pasting from my xterm into vi.  :-):

: hurricane 2465; echo 'comp.lang.awk   288 2 0 The AWK Programming Language' |
sed -e 's/[     ][      ]*[^    ][^     ]*[     ]*[^    ][^     ]*[     ]*[^    ][^     ]*[     ]*/:/'
comp.lang.awk:The AWK Programming Language
: hurricane 2466;

(Note that you don't need to ``double up'' any of the whitespace
expressions except the first.  It doesn't hurt if you do, but it's
not necessary.)

Quote:
>Or, if you cannot abide the inelegance of sed's basic regular
>expressions, use Perl:

>$ echo 'comp.lang.awk 288 2 0 The AWK Programming Language' |
>> perl -wpe 's/^(\S+)\s+\S+\s+\S+\s+\S+\s+(.*)/$1:$2/'
>comp.lang.awk:The AWK Programming Language
>$

>  [snip]

Again, same thing.  Please explain.  Perhaps I'm missing something obvious.

        - Dan C.

- --
Remove the obvious parts to send me email.

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2

iQCVAwUBOLV3+4shWrlwdTE9AQF5pQQAqfjcTJsSDRo+ATHytyuZ6Sk95JetpCAm
enyGjF0D/uYIm04fuUIqk4h8wqUytydBfZFWI45zR+O15cfYfx48T+5TtqgMBKPM
pJAbSJ5fNIBOfejPYoHTyJLbOZ1udMmLfNzsPBW8pXoR9r0CB5M3O4AzkS3iHiVo
+iWLI8kBFAo=
=CkVd
-----END PGP SIGNATURE-----



Mon, 12 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-
What about:
awk '{$2=$3=$4=""; print $0}' in_file



Quote:

> >OK. Try:

> >awk '{printf("%s",$1); for(i=5;i<=NF;++i) printf("%s",$i); print ""}' in_file

> Unfortunately, that reparses the line, so multiple spaces get
> compressed.

> The way to avoid this is to use sub() to change the line.

> Here's an example:

> awk '{a=$0;

>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $2
>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $3
>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $4
>       print a}' infile

> The use of a instead of $0 is probably unnecessary

> Chuck Demas
> Needham, Mass.

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

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


Sent via Deja.com http://www.deja.com/
Before you buy.


Mon, 12 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

>What about:
>awk '{$2=$3=$4=""; print $0}' in_file

That reparses the line too.

echo "a b c d e  f g" | awk '{$2=$3=$4=""; print $0}'

a    e f g

notice that the space between e and f is not preserved, but the spaces
between $2, $3, and $4 still exist.

echo "a b c d e  f g" | awk '{a=$0;
      sub(/[       ][      ]*[^    ][^     ]*/,"",a)
      sub(/[       ][      ]*[^    ][^     ]*/,"",a)
      sub(/[       ][      ]*[^    ][^     ]*/,"",a)
      print a}'

outputs:

a e  f g

which preserves the space between e and f

Chuck Demas
Needham, Mass.

Quote:




>> >OK. Try:

>> >awk '{printf("%s",$1); for(i=5;i<=NF;++i) printf("%s",$i); print ""}' in_file

>> Unfortunately, that reparses the line, so multiple spaces get
>> compressed.

>> The way to avoid this is to use sub() to change the line.

>> Here's an example:

>> awk '{a=$0;

>>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $2
>>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $3
>>       sub(/[       ][      ]*[^    ][^     ]*/,"",a) #removes what was $4
>>       print a}' infile

>> The use of a instead of $0 is probably unnecessary

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

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



Tue, 13 Aug 2002 03:00:00 GMT  
 awk-equivalent to e.g. cut -f5-

Quote:

>I'm using awk to create a .jnewsrc.dsc from the groupinfo-file:
>"comp.lang.awk 288 2 0 The AWK programming language." must become
>"comp.lang.awk:The AWK programming language."
>I think this  ^  colon makes the only use of cut impossible.
>cut -f1,5- only produces "comp.lang.awk The AWK programming language."

>So my solution now is {print $1 ":" substr($0,index($0,$5))}, but i'm a
>bit disappointed that there seems to be no easy way to adress all fields
>from a certain starting-point (like here: $5) up to the end of the record.

I think, the following should work in awk (not testet!):
    printf("%s:%s\n", $1, $5)

Greetings from Switzerland
Happl



Tue, 13 Aug 2002 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. a short cut related question in awk

2. awk cuts stdout to 80 characters ?

3. Seeking equivalent if EOF in awk

4. awk equivalent for casting char to int?

5. tcl equivalent of NR in awk

6. To cut or not to cut?

7. F5 = ASCII 247???

8. Can't get TeraTerm to sent F1-F5 to Clipper EXE

9. CALL X"F5"

10. MF Cobol CALL X"F5"

11. FPS4.0: porting problem frm MS F5.1

12. Making DLL's with MS F5.1

 

 
Powered by phpBB® Forum Software