cat grab.txt
My Dashboard
Fnfjfjf. random test
00:50
1:01:56
My Notes
No data found.
Change Language + English
Submit
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259
Terms And Conditions
84749473837373
Random text fifjfofifofjfkfkf
I need to filter this text after doing the following
Expected output
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259
What have I tried so far?
cat grab.txt | sed -r '/^\s*$/d; /Lecture/,$!d'
After searching for a bit and some trial-error, I am able to remove empty lines and remove all lines before the first occurrence but unable to remove all lines after the last occurrence.
Note - Even tho my existing command is using sed, its fine if the answer is in awk, perl or grep
To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
First, bring your cursor to the line you want to delete. Press the “Esc” key to change the mode. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.
Could you please try following. Written and tested with shown samples with GNU awk
.
awk '
/Lecture/{
found=1
}
found && NF{
val=(val?val ORS:"")$0
}
END{
if(val){
match(val,/.*Lecture [0-9]+/)
print substr(val,RSTART,RLENGTH)
}
}' Input_file
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
/Lecture/{ ##Checking if a line has Lecture keyword then do following.
found=1 ##Setting found to 1 here.
}
found && NF{ ##Checking if found is SET and line is NOT NULL then do following.
val=(val?val ORS:"")$0 ##Creating va and keep adding its value in it.
}
END{ ##Starting END block of this code here.
if(val){ ##Checking condition if val is set then do following.
match(val,/.*Lecture [0-9]+/) ##Matching regex till Lecture digits in its value.
print substr(val,RSTART,RLENGTH) ##Printing sub string of matched values here to print only matched values.
}
}' Input_file ##Mentioning Input_file name here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With