Issue
For instance, there is a piece of text
[task,line:111] first
second
[demo,line:222] first
[test,line:333] first
[task,line:444] first
second
third
[task,line:555] first
I only want the lines with [task] and next lines until another [*] appears. Like below
[task,line:111] first
second
[task,line:444] first
second
third
[task,line:555] first
How can I use awk or other tool in shell script to acomplish it? I just know I can use
awk '/regex/{print $0>"'$output'"}' $file
to get lines with [task] and redirect them to another file.Please help me with this.
Solution
Would you please try the following:
awk '
/^\[/ { # the line starts with "["
if ($1 ~ /^\[task/) f = 1 # set flag for "[task" line
else f = 0 # otherwise reset the flag
}
f {print} # if flag is set, print the line
' input_file > output_file
Then the output_file will look like:
[task,line:111] first
second
[task,line:444] first
second
third
[task,line:555] first
Answered By - tshiono Answer Checked By - Marilyn (WPSolving Volunteer)