Printing previous line based on pattern using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing previous line based on pattern using sed
# 1  
Old 12-23-2009
Printing previous line based on pattern using sed

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:
Code:
----------------------------------------------
#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:
-------------------------------
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....

Last edited by Scott; 12-23-2009 at 11:17 PM.. Reason: Please use code tags
# 2  
Old 12-23-2009
Here is a quick solution, I think the sections are seperated by a blank line, so set the RS=""; FS="\n"

Code:
#!/bin/ksh
read -r heu
awk -v heu=${heu} 'BEGIN{RS=""; FS="\n"}
{
        if(match($0, heu)) {print $0}
}' FILENAME

Should work, let me know.
# 3  
Old 12-23-2009
I worked hard to get the output with the help of sed. So, please write the code in sed only. Also, its not complusion that the lines are separated with blank line. Some times lines are separated with blank lines or some times lines are not separated with blank lines...

Also, when i try to execute your code I am getting below error.

awk: syntax error near line 1
awk: bailing out near line 1

Please help...
# 4  
Old 12-23-2009
hello why not. this simple thing.
Code:
grep -B m -A n 'pattern' infile

m= number of lines you want before the pattern.
n= number of lines you want after the pattern.

Regards
# 5  
Old 12-23-2009
Actually lines are not fixed they change. If the lines are fixed then I can use the command given by you.

please tell me any other solution.
# 6  
Old 12-23-2009
You almost got it. Just change the two regexs from
Code:
/^#[_,BLOCK,UNBLOCK,root, Ineffective]/
/^#[As,output,input]/

to
Code:
/^#\(_\|BLOCK\|UNBLOCK\|root\|Ineffective\)/
/^#\(As\|output\|input\)/

Although the "g" in
Code:
H
g
d

is not necessary.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete the previous line after pattern match?

Team, I am writing a shell script to perform few health checks of the system, where I need to delete the previous line in the text file after pattern match using sed (or) awk. Could you please help me out on this? For example, <td> <td style=color:green align=center> </td> </tr>... (6 Replies)
Discussion started by: Nagaraj R
6 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

6. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

7. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

8. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

9. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

10. UNIX for Dummies Questions & Answers

return previous line for pattern match

Hi, Need some idea on file processing, I have file like below, Processing al sources ... ...No value found : CHECK. Completed comparing all sources. Comparing schedulers... Processing al targets ... ...No value found : From above I need to extract the line where "No value... (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question