Wednesday, August 31, 2022

[SOLVED] How to truncate rest of the text in a file after finding a specific text pattern, in unix?

Issue

I have a HTML PAGE which I have extracted in unix using wget command, in that after the word "Check list" I need to remove all of the text and with the remaining I am trying to grep some data. I am unable to think on a way which can be helpful for removing the text after a keyword. if I do

s/Check list.*//g

It just removes the line , I want everything below that to be gone. How do I perform this?


Solution

You might use q to instruct GNU sed to quit, thus ending processing, consider following simple example, let file.txt content be

123
456
789

and say you want to jettison everything beyond 5, then you could do

sed '/5/{s/5.*//;q}' file.txt

which gives output

123
4

Explanation: for line having 5, substitute 5 and everything beyond it with empty string (i.e. delete it), then q. Observe that lowercase q is used to provide printing of altered line before quiting.

(tested in GNU sed 4.7)



Answered By - Daweo
Answer Checked By - Clifford M. (WPSolving Volunteer)