简体   繁体   中英

AWK number of rows based on a certain row value

I want to keep only certain rows of a .txt file using UNIX Bash command, based of a "code" number:

Name  
Ignore  
2    
1357 1817  
1366 1857  
Name 
Ignore  
3   
293 142  
302 181 
303 181
Name  
Ignore  
7  
1596 1787  
1595 1787  
1594 1787  
1565 1803  
1565 1804  
1565 1805  
1565 1806
Other-name
No-interest
No-interest
No-interest

So only rows with "Name" is of interest. "Other-name" is of no interest. The SECOND ROW following "Name" is a CODE for how many rows are of interest for the "Name". But I dont want to keep all, only THE FIRST AND LAST row following the row with the code. So if the code i 3, I want row 1 and 3 following the row with the code. If the code i 7, I want row 1 and 7 following the row with the code. In the ex. above the output should be:

1357 1817  
1366 1857    
293 142  
303 181
1596 1787    
1565 1806

Thank you very much!

You could try

awk -f ext.awk input.txt

where input.txt is your input file and ext.awk is

/^Name/ {
    getline
    getline
    n=$1
    for (i=1; i<=n; i++) {
        getline
        if (i==1 || i==n) print
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM