grep the line only if next line matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep the line only if next line matches
# 1  
Old 06-29-2007
grep the line only if next line matches

Hi

I have an Input of following sort

AAAA:
ProgName="PROGRAM"
BBBB:
ProgName="BBBBBB"
CCCC:
DDDD:
ProgName="PROGRAM"
SSSS:
ProgName="PROGRAM"
ZZZZ:
ProgName="PROGRAM"

I want to find the Lines which are followed by ProgName="PROGRAM"

Out Put
AAAA:
DDDD:
SSSS:
ZZZZ:
# 2  
Old 06-29-2007
Code:
awk '/ProgName="PROGRAM"/ { print prv_line; next } { prv_line = $0 }' inputfile

# 3  
Old 06-29-2007
Code:
sed -n "N;/\n.*PROGRAM/P;D" filename

# 4  
Old 06-29-2007
perl -ne 'print if /THIS/../THAT/'
# 5  
Old 06-29-2007
Thanks anbu aigles
# 6  
Old 07-10-2007
Hi
Im very new to this Smilie
i kinda have the same problem but I need lines followed by empty lines.
Input looks like this:

d:/bla1/bla/bla
d:/bla2/bla/bla

some text

d:/bla3/bla/bla
d:/bla4/bla/bla

I need the line d:/bla2/bla/bla

Can anyone help?

Thanks
# 7  
Old 07-10-2007
awk '/^$/ { print prv_line; next } { prv_line = $0 }' file_name
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Print line 1 if line 3 matches of the output

Hi I want to extend following command so that on the basis of "Branch: ****" on the third line I can grep and print name of the file on the first line. cat .labellog.emd | grep DA2458A7962276A7E040E50A0DC06459 | cut -d " " -f2 | grep -v branch_name | xargs -I file <command to describe> file ... (1 Reply)
Discussion started by: ezee
1 Replies

2. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

3. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. UNIX for Dummies Questions & Answers

print line that matches and next line, too

I'm using sh on hp-ux and want to find / print a line that matches 132.101- and the next line, too. grep -A isn't supported on hp-ux, so I'm trying awk and sed. The code below works but only prints the first occurence. I need all matches from the file. awk... (2 Replies)
Discussion started by: Scottie1954
2 Replies

5. Shell Programming and Scripting

How to substitute a line that matches an expression with another line

I need some help. I have a file (all.txt) whereby I want to substitute using sed/awk all lines that matches an expression with another line with different expression i.e subtitute expression, database_id: filename; WITH database_id: PY; There are many occurrences of the expression... (4 Replies)
Discussion started by: aimsoft
4 Replies

6. UNIX for Dummies Questions & Answers

if line matches then

I have a file that I need to read each line and see if the line begins with a certain keyword. I can't seem to find something that works. #!/bin/ksh # while read i do if ] then echo "SE2 WORKS" elif ] then echo "CISCO1 WORKS" fi done < tmp/diff The file looks something like SE1 SE2... (2 Replies)
Discussion started by: numele
2 Replies

7. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

8. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

9. UNIX for Advanced & Expert Users

Inserting a line before the line which matches the patter

Hi Is there any command where we can insert a line "2|||" before every line starting with "3|" my input is as follows 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 2|EN_GB||Electrogalvanize 0.5 m2 ( Renault ) 1|ETG|12345 3|88.51|||GBP|| desired output... (10 Replies)
Discussion started by: laxmi131
10 Replies

10. Shell Programming and Scripting

Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier. What I would like to do is have each line that looks like this: setmqaut -m... (4 Replies)
Discussion started by: sysera
4 Replies
Login or Register to Ask a Question