|
|
Selective printing revisited
Hey all, I'm having an issue with getline I'm hoping someone can help
me with. I have a sample file with lines numbered:
1 Value A
2 Value B
3 Value C
4 Value D
5 Value E
6 etc
7 etc
8 etc
9 etc
10 etc
11 etc
12 etc
13 etc
14 etc
15 Decisive Regex
I'm looking to print ONLY line 1 if line 15 is '/Decisive Regex/'
I tried the awk '/Decisive Regex/{print x};{x=$0}' in a combination of
ways, but I can't get it to go up to line 1
|
|
0
|
|
|
|
Reply
|
dsphunxion
|
12/29/2010 6:26:39 PM |
|
In article <38458941-9b90-4691-a365-95a48826acad@v12g2000vbx.googlegroups.com>,
dsphunxion <dsphunxion@e-fensive.net> wrote:
>Hey all, I'm having an issue with getline I'm hoping someone can help
>me with. I have a sample file with lines numbered:
>
>1 Value A
>2 Value B
>3 Value C
>4 Value D
>5 Value E
>6 etc
>7 etc
>8 etc
>9 etc
>10 etc
>11 etc
>12 etc
>13 etc
>14 etc
>15 Decisive Regex
>
>I'm looking to print ONLY line 1 if line 15 is '/Decisive Regex/'
>
>I tried the awk '/Decisive Regex/{print x};{x=$0}' in a combination of
>ways, but I can't get it to go up to line 1
The simplest way to approach this sort of problem, as a beginner and
assuming that your file isn't too big (which, nowadays, means less than
a few gigabyes), is to do it like this:
{ A[NR] = $0 }
END { Do whatever analysis you want here }
In your case, your END block would be:
END { if (A[15] ~ /Decisive Regex/) print A[1] }
--
Religion is regarded by the common people as true,
by the wise as foolish,
and by the rulers as useful.
(Seneca the Younger, 65 AD)
|
|
0
|
|
|
|
Reply
|
gazelle
|
12/29/2010 6:57:06 PM
|
|
On Wed, 29 Dec 2010 10:26:39 -0800 (PST)
dsphunxion <dsphunxion@e-fensive.net> wrote:
> Hey all, I'm having an issue with getline I'm hoping someone can help
> me with. I have a sample file with lines numbered:
>
> 1 Value A
> 2 Value B
> 3 Value C
> 4 Value D
> 5 Value E
> 6 etc
> 7 etc
> 8 etc
> 9 etc
> 10 etc
> 11 etc
> 12 etc
> 13 etc
> 14 etc
> 15 Decisive Regex
>
> I'm looking to print ONLY line 1 if line 15 is '/Decisive Regex/'
>
> I tried the awk '/Decisive Regex/{print x};{x=$0}' in a combination of
> ways, but I can't get it to go up to line 1
Not generic, but maybe good enough for this time:
awk 'NR==1{first=$0} NR==15 && /Decisive Regex/{print first;exit}'
|
|
0
|
|
|
|
Reply
|
pk
|
12/29/2010 7:33:11 PM
|
|
On Wed, 29 Dec 2010 19:33:11 +0000 pk <pk@pk.invalid> wrote:
> awk 'NR==1{first=$0} NR==15 && /Decisive Regex/{print first;exit}'
In fact, I seem to understand that you can exit anyway after line 15, so
awk 'NR==1{first=$0} NR==15 {if(/Decisive Regex/)print first;exit}'
|
|
0
|
|
|
|
Reply
|
pk
|
12/29/2010 7:45:00 PM
|
|
|
3 Replies
169 Views
(page loaded in 0.146 seconds)
Similiar Articles:7/20/2012 9:36:08 AM
|
|
|
|
|
|
|
|
|