-2

One needs to extract lines from a txt file, to other files, based on the following scenario:

The first pattern to match against, will be always the line that starts with the word identifier, then the second pattern, will be the first line, that starts with 000:

in the below example, it will be

000: thislineneedstobeextracted  

after we have written the output to the first file, we move to extract the lines that start with the word identifier, and the lines that start again with 000: but this time, for the second unique occurrence found in the file, in our example,

000: alsothislineneedstobeextracted  

and the output will be written to the second file, etc.

sample input

identifier 767  
linetobeignored  
anotherlinetobeignored  
000: thislineneedstobeextracted  
000: alsothislineneedstobeextracted  
blankline  
identifier 7686    
linetobeignored  
anotherlinetobeignored  
000: thislineneedstobeextracted  
000: alsothislineneedstobeextracted  
000: ayetanotherlineneedstobeextracted  

sample output file1.txt:

000: thislineneedstobeextracted  
identifier 767  
identifier 7686    

sample output file2.txt:

000: alsothislineneedstobeextracted  
identifier 767  
identifier 7686    
  • 1
    What problems are you having with your code? – John1024 Sep 21 '16 at 21:55
  • 1
    Did you do anything yourself or do you expect us to do your homework? – ukll Sep 21 '16 at 23:42
0

Try with awk

awk '
    /^id/{
        if(line[1]){
            for(n in line)print line[n],i,$0 >> "file"n".txt"
            delete(line)
            }
        c=0
        i=$0
        }
    /^000:/{
        line[++c]=$0
        }
    END{
        if(line[1]){
            for(n in line)print line[n],i,$0 >> "file"n".txt"
        }
    ' OFS=\\n txt.file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.