Hi,
I have a written a shell script to get the previous line based on the pattern.
For example if a file has below lines:
----------------------------------------------
#UNBLOCK_As _per
#As per
205.162.42.92
#BLOCK_As_per
#-----------------------
#input checks
abc.com
google.com
121.223.213.99
#BLOCK_As_per
#-----------------------
#output checks
12.91.77.123
200.55.185.74
----------------------------------------------
I want to get the line of a pattern I am searching for along with the previous lines also based on pattern only.
From the above lines:
If I am searching for pattern "200.55.185.74" I want this pattern to be printed along with the "#output" and "#BLOCK" lines.
I am able to get the output what I want but few times for few patterns script is not able to process it.
I am using sed command. Below code is working for all the patterns but when I am trying to search "200.55.185.74" its getting previous line "#output checks" but its not printing "#BLOCK_As_per" line.
I want the output in the below manner "
#BLOCK_As_per
#output checks
200.55.185.74
Below is code:
-------------------------------
read -r heu
net=$(echo "$heu" | sed 's/[]\[:?+*/^\$!|.]/\\&/g')
$(sed -n -e '
/^#[_,BLOCK,UNBLOCK,root, Ineffective]/h
/^#[As,output,input]/ {
H
g
d
}
/'"$net"'/ {
H
g
p
}' file)
Please help me... should I use branching in sed. If yes them please tell me how to use it.
appreciate your help....