Home > Archive > AWK > February 2005 > easy: get first column that matches ...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
easy: get first column that matches ...
|
|
| Gernot Frisch 2005-02-01, 3:56 pm |
| Hmmm.. this must be easy:
Assume a lines:
TEXT "Some Text" ID_SOME
#define ID_CS 12312
I'd like to get the tokens that start with "ID" - what's the easiest
way? split?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
| |
| Stephane CHAZELAS 2005-02-01, 3:56 pm |
| 2005-02-1, 14:49(+01), Gernot Frisch:
> Hmmm.. this must be easy:
>
> Assume a lines:
>
> TEXT "Some Text" ID_SOME
> #define ID_CS 12312
>
> I'd like to get the tokens that start with "ID" - what's the easiest
> way? split?
[...]
That's not a awk solution, but I would do it this way:
tr -s ' \t' '\n\n' | grep '^ID'
or
tr -cs 'a-zA-Z0-9-' '[\n*]' | grep '^ID'
to answer the question in the subject:
{
for (i = 1; i <= NF; i++) {
if ($i ~ /^ID/) {
print $i
break
}
}
}
--
Stéphane
| |
| William James 2005-02-01, 8:55 pm |
| Instead of split, use shatter.
z {
z shatter( $0, a, "(^|[^a-zA-Z_0-9])ID[a-zA-Z_0-9]*" )
z for (i=2;i in a;i+=2)
z { token = a[i]; sub(/^[^a-zA-Z_0-9]/, "", token)
z print token
z }
z }
z
z # Produces array of nonmatching and matching
z # substrings. The size of the array will
z # always be an odd number. The first and the
z # last item will always be nonmatching.
z function shatter( s, array, re )
z { gsub( re, "\1&\1", s )
z return split( s, array, "\1" )
z }
| |
| Ed Morton 2005-02-02, 3:56 am |
|
Gernot Frisch wrote:
> Hmmm.. this must be easy:
>
> Assume a lines:
>
> TEXT "Some Text" ID_SOME
> #define ID_CS 12312
>
> I'd like to get the tokens that start with "ID" - what's the easiest
> way? split?
No, just redefine your RS so every "field" gets treated as a separate
record, then look for records that start with "ID":
gawk -vRS="[[:space:]]" '/^ID/'
Regards,
Ed.
|
|
|
|
|