Loop - Retrieve the text for recording until the next recording

My dummy file looks like this:

>Tom text text text, year = 16 text text text Grades . . . . 10 Grades . . . . 9 >Ann text text text, year = 17 text Grades . . . . 10 Grades . . . . 10 Grades . . . . 9 

I want to:
For each name, assign a value, specify the year and last number in the classes. To output as follows:

  1 Tom 1 year = 16 1 10 1 9 2 Ann 2 year = 17 2 10 2 10 2 9 

I don’t know how I can change the text from the record to the next record. It may be possible grep '^>' -An , but the line number is different for each entry.

+4
source share
2 answers

Assuming there are no leading spaces in your real files, here is one way: awk :

 awk 'sub(/^>/,"") { print ++i, $0 } /Grades/ { print i, $NF } /year =/ { print i, "year = ", $NF }' file 

Results:

 1 Tom 1 year = 16 1 10 1 9 2 Ann 2 year = 17 2 10 2 10 2 9 
+4
source
 perl -nE 'BEGIN{ $/ = ">"; $i=0} chomp; next unless length; $i++; say "$i $_" for /(.+)/, /(year\s*=.+)/, /grades.+?(\d+)/gi' file 
+2
source

Source: https://habr.com/ru/post/1484411/


All Articles