Home > Archive > AWK > October 2006 > locate strings --newbies need hand
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 |
locate strings --newbies need hand
|
|
| linuxnb@gmail.com 2006-10-30, 7:01 pm |
| Hi, Dear awk gurus,
I have a problem to use awk to clarify the exact string in a file,
for instance, the file looks like:
abc bcd cdf bcv thr
wbc bsd dgfs cdf bdv thuy
ok, in the 2 lines file, I need print the result:
bcv
bdv
I try to use substr( ), but have no luck, I am new to awk.newbies
So I need your any tips, thanks a lot,
Zhou
| |
| Kenny McCormack 2006-10-30, 7:01 pm |
| In article <1161763217.443506.54910@i42g2000cwa.googlegroups.com>,
linuxnb@gmail.com <linuxnb@gmail.com> wrote:
>Hi, Dear awk gurus,
>
>I have a problem to use awk to clarify the exact string in a file,
>
>for instance, the file looks like:
>
>abc bcd cdf bcv thr
>wbc bsd dgfs cdf bdv thuy
>
>ok, in the 2 lines file, I need print the result:
>
>bcv
>bdv
I suppose:
{ print $(NR+3) }
| |
| Thomas Weidenfeller 2006-10-30, 7:01 pm |
| linuxnb@gmail.com wrote:
> for instance, the file looks like:
>
> abc bcd cdf bcv thr
> wbc bsd dgfs cdf bdv thuy
That example is not very helpful. Is there any order or structure in the
file? E.g. could it be that you always want the second-last field? That
would be
{ print $(NF-1) }
/Thomas
| |
| zhouzhenzj@gmail.com 2006-10-30, 7:01 pm |
|
Thomas Weidenfeller wrote:
> linuxnb@gmail.com wrote:
>
> That example is not very helpful. Is there any order or structure in the
> file? E.g. could it be that you always want the second-last field? That
> would be
>
> { print $(NF-1) }
>
>
> /Thomas
thanks a lot, maybe I didn't give more clear example for this kind
request, if the file looks like:
abc bcd cdf bcv thr
wbc bsd dgfs cdf bdv thuy
ags cdf kjdg jindh lkljh lkjh
then print:
bcv
bdv
kjdg
now the example is more clear that the result will be just after "cdf",
now how to do that?
Thanks a lot,
Zhou
| |
| Lorenz 2006-10-30, 7:01 pm |
| zhouzhenzj@gmail.com wrote:
>thanks a lot, maybe I didn't give more clear example for this kind
>request, if the file looks like:
>abc bcd cdf bcv thr
>wbc bsd dgfs cdf bdv thuy
>ags cdf kjdg jindh lkljh lkjh
>
>then print:
>bcv
>bdv
>kjdg
>
>now the example is more clear that the result will be just after "cdf",
>now how to do that?
gawk "match($0, /cdf +([^ ]+)/, a) {print a[1]}" <input file>
Lorenz
| |
| Ed Morton 2006-10-30, 7:01 pm |
| zhouzhenzj@gmail.com wrote:
> Thomas Weidenfeller wrote:
>
>
>
> thanks a lot, maybe I didn't give more clear example for this kind
> request, if the file looks like:
> abc bcd cdf bcv thr
> wbc bsd dgfs cdf bdv thuy
> ags cdf kjdg jindh lkljh lkjh
>
> then print:
> bcv
> bdv
> kjdg
>
> now the example is more clear that the result will be just after "cdf",
> now how to do that?
Depending on your requirements for what to do if/when cdf is the last
field, this might work for you:
awk '{for (i=1;i<NF;i++) if ($i == "cdf") print $(i+1)}' file
Ed.
| |
| Kenny McCormack 2006-10-30, 7:01 pm |
| In article <kgjuj2t0hgdnajf30mutdranki5jcmj6v2@4ax.com>,
Lorenz <lorenznl@yahoo.com> wrote:
>zhouzhenzj@gmail.com wrote:
>
>gawk "match($0, /cdf +([^ ]+)/, a) {print a[1]}" <input file>
That's cute. I had forgotten about GAWK having that match() extension.
Here's a somewhat more straightforward approach (same general theme):
{split(substr($0,index($0," cdf ")),a);print a[2]}
|
|
|
|
|