1752290 Members
5312 Online
108786 Solutions
New Discussion юеВ

grep command

 
SOLVED
Go to solution
Unix or Linux?
Frequent Advisor

grep command

I have a very large file that I wish to retrieve information from. The file contains data that can be repeated many times such as.

USERNAME:john.doe
EMAIL:jonh.doe@yahoo.com
ADDRESS:somewhere
PHONENUMBER:12345
USERNAME:john.doe
EMAIL:jonh.doe@yahoo.com
ADDRESS:somewhere
PHONENUMBER:12345

How do I grep the file so that the output will order the data in the following way:The output from grep should be:
1)USERNAME:john.doe
2)EMAIL:jonh.doe@yahoo.com
3)PHONENUMBER:12345
4)USERNAME:john.doe
5)EMAIL:jonh.doe@yahoo.com
6)PHONENUMBER:12345



18 REPLIES 18
Geoff Wild
Honored Contributor
Solution

Re: grep command

Use egrep:

egrep "USERNAME|EMAIL|PHONE" file

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: grep command

You don't. This isn't a job for grep. This is something that is best tackled with Perl, awk, or sed --- probably in that order of preference although for a novice awk is probably the easist to learn.
If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: grep command

It looks like you're just removing the ADDRESS entry so pretty simple:

grep -v "^ADDRESS:" filename

-v prints all lines that don't match the pattern.

^ designates the pattern should be at the beginning of the line.
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: grep command

Hi:

Filters like 'grep' make one-pass through a file, and hence your output will occur in the order the matches appear in the input source.

You can do:

# grep -e USERNAME -e EMAIL -e PHONENUMBER filename

...and you will see the order you posted as ouput if that's the sequential order of the input file.

Regards!

...JRF...
Unix or Linux?
Frequent Advisor

Re: grep command

Wow! Thank you for all the replies.

Is there any advantage from using grep -e to using egrep?
How do I number the lines followed by a bracket?
Chan 007
Honored Contributor

Re: grep command

Try,

egrep "USERNAME|EMAIL|PHONENUMBER"

Chan
Geoff Wild
Honored Contributor

Re: grep command

Chan - that's exactly the same as what I already gave...no need to reiterate it...

0 points for this reply.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Jeff_Traigle
Honored Contributor

Re: grep command

INDEX=1
grep -v "^ADDRESS:" filename | while read LINE
do
echo "${INDEX})${LINE}"
INDEX=$((${INDEX}+1))
done
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: grep command

Hi:

On HP-UX 'grep -E' (uppercase!) provides the same features as 'egrep'.

http://www.docs.hp.com/en/B2355-60103/grep.1.html

Regards!

...JRF...