getting two lines by using grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting two lines by using grep command
# 1  
Old 01-04-2011
Java getting two lines by using grep command

i hav a text file(abc.txt) in which it have 2 lines as shown

Number of agencies didnt send the file= 0

sum of reject files= 0


######
to get one line from the text file ... i use grep command
grep 'Number of agencies didnt send the file= 0' abc.txt

but for both the lines ..... what i want to use.......

can any one suggest
# 2  
Old 01-05-2011
try:

Code:
egrep 'Number of agencies didnt send the file= 0|sum of reject files= 0' abc.txt

# 3  
Old 01-05-2011
Code:
awk '/Number of agencies didnt send the file= 0/||/sum of reject files= 0/' abc.txt

# 4  
Old 01-05-2011
@rdcwayx
@anchal khare

its not working
can u plz solve it..... its showing not found....
but the lines are in the file
# 5  
Old 01-05-2011
From what I can see, Anchal Khare has a good solution. Read the grep/egrep/fgrep manual pages. The command given is saying:-
Code:
Get "first bit of text I'm searching for" or "second bit of text I'm searching for" from file abc.txt

The command egrep (or grep -E on AIX) allows you so speficy an extended regular expression as the search criteria, so you can set up both phrases.

Perhaps you could shrink the search to just
Code:
grep "=" abc.txt

.... unless that generates lots of wrong hits. It should find every line containing the equasl sign.

It's difficult to tell with such limited input how to neatly phrase the expression.


I cannot really comment on the awk solution because I'm not very practised.


I hope that this helps

Robin
Liverpool/Blackburn
UK
# 6  
Old 01-05-2011
@rbatte1:

i have a text file abc.txt as shown below
##################### text file begins####################
MONTH_20100801_1_IN.txt 2310 1042 0
MONTH_20100803_1_IN.txt 0708 708 0
MONTH_20100804_1_IN.txt 023 023 0
Number of agencies didnt send the file=0
sum of reject files= 0
MONTH_20100802_1.txt
MONTH_20100802_1_IN.txt
MONTH_20100805_1_IN.TXT
####################### end #########################
i need to run a script based on the values of those 2 lines
Number of agencies didnt send the file=0
sum of reject files= 0
if they both are zero i hav to run a script

######### for only one statement ...i wrote
grep 'Number of agencies didnt send the file=0' abc.txt
RC=$?
if [$RC -ne 0]
####some message
else
#### run script
fi
################################
i wann to worte like this if both the statements are zero....
# 7  
Old 01-05-2011
One way is to use egrep and count the results.
The escaped $ (\$) means "end of line" in case the program outputs numbers above zero with leading zeros.

Code:
found_count=`egrep "Number of agencies didnt send the file=0\$|sum of reject files= 0\$" myfile.txt | wc -l`
#
if [ ${found_count} -eq 2 ]
then
        echo "Run my script"
fi

Note in your example data there are subtle differences in spacing near the equals signs. The "egrep" reacts to the exact spacing.
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Need to grep two lines from the topas command through a Korn Script

I need to grep two lines from topas command in one of my AIX server. Topas Monitor for host: semas001 EVENTS/QUEUES FILE/TTY Wed Jan 29 10:12:06 2014 Interval: 2 Cswitch 764 Readch 4473.1K Syscall 5166 ... (4 Replies)
Discussion started by: rpm120
4 Replies

2. AIX

Print output of grep command in multuple lines

HI All, I am using grep command to serach a pattern in a list of files and storing the output in a variable. Then i am applying some logic on that variable to get the required output. But Thing is that when the pattern is present mutiple times in a file, i am getting the output of grep in a... (3 Replies)
Discussion started by: goutam sahoo
3 Replies

3. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

4. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

5. UNIX for Dummies Questions & Answers

Grep command to remove blank lines

The following grep command grep -v "^$" filename > newfilename does not populate the new file with any data. I see it search the entire input file but the output file never gets filled. Is this not the correct command for what Im looking to do? (2 Replies)
Discussion started by: aispg8
2 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. Shell Programming and Scripting

Help required on grep command(Skip the first few lines from printing in the output)

Hi experts I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output. I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string.... (7 Replies)
Discussion started by: ks_reddy
7 Replies

8. Shell Programming and Scripting

print multiple lines using the grep command.

Hi All, Please find my piece of code below. I am trying to grep the word SUCCESS from $LOGFILE and storing in the grepvar variable. And i am placing that variable in a file. Now if i open the file, i can see the four lines but not in seperate four line s but in a paragraph. If am mailing that log... (8 Replies)
Discussion started by: intiraju
8 Replies

9. UNIX for Dummies Questions & Answers

How to list file names instead of lines when grep a gzcat command?

Hi, I want to list filenames instead of lines when i search in compresed files for a string. #gzcat *.gz | grep -l 12345 gives me: <stdin> Anyone got a solution on this problem? (2 Replies)
Discussion started by: HugoH
2 Replies

10. Shell Programming and Scripting

number of lines returned from a grep command

hi all, from a shell (ksh) script, i am doing a 'grep'. how do i find out the number of lines returned from that 'grep' command ?? thanks in advance. (4 Replies)
Discussion started by: cesarNZ
4 Replies
Login or Register to Ask a Question