Questions on GREP command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Questions on GREP command
# 1  
Old 12-24-2007
Question Questions on GREP command

Hi,

Is it possible to display a specific number of lines starting from a line having a particular text using grep command?

e.g. I have a text file with the contents below:

AAA
BBB
CCC
DDD
EEE
FFF

I want to display 3 lines starting with the line having "BBB" to get the result below:

BBB
CCC
DDD


Steve
# 2  
Old 12-24-2007
grep -A 2

The message you have entered is too short. Please lengthen your message to at least 10 characters.
# 3  
Old 12-25-2007
DeepakS,

Thanks for your reply but I'm using HP-UX B.11.11 and that command doesn't work.

I know that it can be done using "SED" with the command below but I'm looking for a shorter method.

Code:
sed -n '/BBB/,$p' FILENAME | sed -n '1,3p'

# 4  
Old 12-25-2007
I guess it depends on what you're trying to do. If you just need to save a few keystrokes throw this into a script:

Code:
#! /bin/bash

LINES=$(($1+1))

sed -n "/$3/,${LINES}p" $2

Parameter 1 is the number of lines to display, parameter 2 is the filename, and parameter 3 is the search string. Not as neat as GNU but it works.

Last edited by DeepakS; 12-25-2007 at 02:10 AM.. Reason: Fixed script, it only searched for BBB :p
# 5  
Old 12-25-2007
In addition to my original question I want to find multiple occurrences of the string.

e.g. I have a file called 20071225.log with the contents below:

SUCCESS
1234
2433
SUCCESS
4321
6523
ERROR
5678
8423
SUCCESS
7890
5324
ERROR
3456
3214

I want to display 3 lines below all the occurrences of the line having the string "ERROR" to get the output below:

ERROR
5678
8423
ERROR
3456
3214


I can get the above result with the shell below but I would like a shorter method possibly in one line of command :


Code:
STRING=$1
FILE=$2

DONE=N

while [ $DONE = N ]
do
  sed -n "/$STRING/,\$p" $FILE | sed -n '1,3p'
  sed -n "/$STRING/,\$p" $FILE | sed '1,3d' > $$.txt
  mv $$.txt $FILE
  [[ `cat $FILE | wc -l` -eq 0 ]] && DONE=Y
done

# 6  
Old 12-25-2007
Code:
sed -n '/ERROR/ { N; N; /./p }' foo

This will match ERROR followed by two lines.
# 7  
Old 12-25-2007
Code:
awk 'f&&f--; /BBB/{f=3} ' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk questions using sort and grep

1. The problem statement, all variables and given/known data: So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a... (4 Replies)
Discussion started by: jamesb18
4 Replies

2. Shell Programming and Scripting

Sed/grep questions

Hi. I have a txt file. I need to make a copy of the lines which are beginning with a mobile phone number, or a fix phone number. I have to copy thoose lines in numbers.txt, after that i have to delete then from the originally file. In numbers.txt i need to write a prefix before each number. if the... (1 Reply)
Discussion started by: T720
1 Replies

3. UNIX for Dummies Questions & Answers

Couple of questions wth grep/sort

I have different things that I was trying to do but am kind of struggling with this since I'm a Linux noob. The backround is that I have two files with student names in the same directory, and each file lists the student name, their major and their grade level. What is the most efficient way to... (6 Replies)
Discussion started by: tastybeer
6 Replies

4. Shell Programming and Scripting

grep and sed exact match questions

This post was previously mistaken for homework, but is actually a small piece of what I working on at work. Please answer if you can. QUESTION1 How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (2 Replies)
Discussion started by: thibodc
2 Replies

5. Shell Programming and Scripting

Some questions about grep/awk

Hi guys. I need to filter some values from a number of log files. One of the files is: Interconnect Utilisation Results: Achieved Maximum Number of Concurrent Connections: 17 Statistics for Average Number of Concurrent Connections: Point Estimation: Confidence Interval: ... (2 Replies)
Discussion started by: Faaz0
2 Replies

6. Shell Programming and Scripting

Grep questions

Hello All, I have few of questions related to Grep given below: 1. Like Perl, is it possible in Grep to negate characters in square brackets. For example in Perl, if '^' is used inside '' then it acts as a negation characters. Can same be achieved through Grep's regular expression. 2. How... (9 Replies)
Discussion started by: paragkalra
9 Replies

7. UNIX for Dummies Questions & Answers

Using Grep Questions

Hello All, 1.) I am searching for ".exe" in a text file 2.) I need to search for a hexadecimal entree of at least four digits (8 Replies)
Discussion started by: Omega1589
8 Replies

8. UNIX for Dummies Questions & Answers

grep questions

I have the data file: A 1 2 3 BBB 4 5 6 A 7 8 9 I want to grep "A" then-skip a line-then-add two sublines: I my command: grep +3 "A" datafile (8 Replies)
Discussion started by: bobo
8 Replies

9. UNIX for Dummies Questions & Answers

Unpratical SED and GREP questions

Hello every one, I have read a little about SED and GREP but I do not know how to do this: Using SED or GREP: "reverse all three letter words" "replace the last two digits in any string of digits by zeros (0)" "remove lines that start and end with the same word" and I have more like... (5 Replies)
Discussion started by: Lem2003
5 Replies

10. UNIX for Dummies Questions & Answers

Simple grep questions

Hi all, My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question