Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting a group of records based on a substring header 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I have a data file as follows:

A11111 1xxxxxxxxxxxxxxxxxxxxxxxxxxx
B11111 2xxxxxxxxxxxxxxxxxxxxxxxxxxx
C11111 3xxxxxxxxxxxxxxxxxxxxxxxxxxx
D11111 4xxxxxxxxxxxxxxxxxxxxxxxxxxx
sdfsdf
sdfsdf
sdfsdf
sdfsdf
A22222 1xxxxxxxxxxxxxxxxxxxxxxxxxxx
B22222 2xxxxxxxxxxxxxxxxxxxxxxxxxxx
C22222 3xxxxxxxxxxxxxxxxxxxxxxxxxxx
D22222 4xxxxxxxxxxxxxxxxxxxxxxxxxxx
sdfsdf
sdfsdf
sdfsdf
sdfsdf
A33333 1xxxxxxxxxxxxxxxxxxxxxxxxxxx
B33333 2xxxxxxxxxxxxxxxxxxxxxxxxxxx
C33333 3xxxxxxxxxxxxxxxxxxxxxxxxxxx
D33333 4xxxxxxxxxxxxxxxxxxxxxxxxxxx
sdfsdf
sdfsdf
sdfsdf
sdfsdf

I want to extract a group of records that begin with A2222
and all records that follow until A3333

Thanks for your help
 
Hi mrr,

Try this.

{
if ($1=="A22222") fnd = 1
if (fnd) {
if ($1=="A33333") exit
print
}
}

Hope this helps.
CaKiwi
 
Cakiwi,
Could you loop through this?
This definitely doesn't work.
I also tried a sort that didn't work.
ex:
if ($0 ~ /A2+/) {
start = NR
}
if ($0 ~ /A3+/) {
end = NR
}
do
print $0
}
while (NR < end && NR > start)

 
marsd,
The problem with your code is that when you reach the record containing A22222, 'end' will still be zero so your loop will not be executed. Also, don't forget that this code is executed once for every record, so you don't really want a loop that will print out each record multiple times.
CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top