这条线可能对你有用:
awk '/^1/{i++;a[i]=$0;next}i>1{for(x=1;x<=i;x++)print a[x]}{i=0;delete a}' file
例子:
kent$ cat fi
1xxxxxxx xxxxx xxxxx
2yyyyyyy yyyyy yyyyy
2yyyyyyy yyyyy yyyyy
1xxxxxxx xxxxx xxxxx
2yyyyyyy yyyyy yyyyy
2yyyyyyy yyyyy yyyyy
1here
1we
1want
2yyyyyyy yyyyy yyyyy
1these
1lines
1too
2yyyyyyy yyyyy yyyyy
kent$ awk '/^1/{i++;a[i]=$0;next}i>1{for(x=1;x<=i;x++)print a[x]}{i=0;delete a}' fi
1here
1we
1want
1these
1lines
1too
解释:
awk
'/^1/{i++;a[i]=$0;next} #if line starts with 1, ++i, save it in array a, read next line
i>1{for(x=1;x<=i;x++)print a[x]} #if till here, line doesn't start with 1. if i>1, it means, there are atleast 2 consecutive lines starting with 1, in array a. print them out
{i=0;delete a} #finally clear i and array a