Zombo
Zombo

Reputation: 1

First match after match

Given this file, I would like to print the first install: line that comes after the @ bash line. Please see the file for complete input, but here is a sample

@ base-files
; few lines
install: x86_64/release/base-files/base-files-4.2-3.tar.xz 46428 6372358800e589b
; couple of lines
install: x86_64/release/base-files/base-files-4.1-1.tar.bz2 49519 f91ed6eab060c3

@ bash
; few lines
install: x86_64/release/bash/bash-4.1.17-9.tar.xz 1107128 e49b8d67d59d8617dfa31c
; couple of lines
install: x86_64/release/bash/bash-4.1.16-8.tar.xz 1106812 5aa652ddc0a5d65f4af1e2
source: x86_64/release/bash/bash-4.1.16-8-src.tar.xz 6614280 79bb3ddc67d8f0d3da6

@ bash-completion
; few lines
install: x86_64/release/bash-completion/bash-completion-1.3-1.tar.bz2 117489 538
source: x86_64/release/bash-completion/bash-completion-1.3-1-src.tar.bz2 216503

Output should be

install: x86_64/release/bash/bash-4.1.17-9.tar.xz 1107128 e49b8d67d59d8617dfa...

I have this command, but it prints all lines inclusive of the two.

awk '/@ bash$/,/install:/' setup.ini

In addition to the answers I created this one

awk '$1=="@" {c=$2} $1=="install:" && c=="bash" {print;exit}'

Upvotes: 1

Views: 179

Answers (3)

Zombo
Zombo

Reputation: 1

awk '$1=="@" {c=$2} $1=="install:" && c=="bash" {print;exit}'
  • If field one is @, save field two to variable c
  • If field one is install: and c is bash, print and exit

Upvotes: -1

anubhava
anubhava

Reputation: 786359

You can use:

awk '/@ bash$/{p=1} p&&/install:/{print; exit}' setup.ini
install: x86_64/release/bash/bash-4.1.17-9.tar.xz 1107128 e49b8d67d59d8617dfa31c

Upvotes: 5

Jonathan Leffler
Jonathan Leffler

Reputation: 755114

awk '/^@ bash/ { p = 1 } /^install:/ && p == 1 { print; p = 2 }'

The variable p defaults to 0; when the @ bash line is read, set it to 1; when the next install line is read, print it and set p to 2 so no other install lines will be printed. You could also exit instead of setting p = 2, I suppose.

Upvotes: 4

Related Questions