Reputation:
I want to extract all lines before and after I find "ACCCC" until the blank lines
Here is a sample data
ABCDEFG
ABASLDKJ
ASDASKKK
ASDASDAS
ACCCC
ASDASDAS
ASDASDAS
ASDASDAA
I tried using sed
sed -n '/ACCC/,/^$/p' test
#ACCCC
#ASDASDAS
#ASDASDAS
But using this I do not get the above lines.
I know I could use grep -A -B but the number of lines before and after the pattern might change in my original data
I need to extract
ABASLDKJ
ASDASKKK
ASDASDAS
ACCCC
ASDASDAS
ASDASDAS
Upvotes: 3
Views: 1662
Reputation: 58478
This might work for you (GNU sed):
sed '/\S/H;//d;x;/^\n.*ACCC/s/.//p;x;h;d' file
If the current line contains a non-space character, append it to the hold space (HS) and then delete it. Otherwise, swap to the HS and check if its contents contains the required string beginning with an empty line and if so remove the empty line and print the remaining contents. In all cases replace the HS with the current line and then delete it.
Upvotes: 1
Reputation: 23677
Use awk's paragraph mode (one or more empty lines act as record separator)
$ awk -v RS= '/ACCCC/' ip.txt
ABASLDKJ
ASDASKKK
ASDASDAS
ACCCC
ASDASDAS
ASDASDAS
-v
command line option helps to set value to a variableRS
is input record separator, whose default value is newline characterFrom awk manual
By a special dispensation, an empty string as the value of RS indicates that records are separated by one or more blank lines. When RS is set to the empty string, each record always ends at the first blank line encountered. The next record doesn’t start until the first nonblank line that follows. No matter how many blank lines appear in a row, they all act as one record separator. (Blank lines must be completely empty; lines that contain only whitespace do not count.)
Upvotes: 3