1828159 Members
2635 Online
109975 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...
Chan 007
Honored Contributor

Re: grep command

Geoff,

I opened two 3 sessions and assigning points on other threads and forget to submit this...

I have not seen your reply..
I am fine with big fat "0"

Chan
Unix or Linux?
Frequent Advisor

Re: grep command

Hi Jeff T,


That was only a small portion of the file that I displayed. The file contains many more fields (that I dont know the names of)appart from ADDRESS that I dont need. I only know the names of the fields that I need hence deleting is not an option.
James R. Ferguson
Acclaimed Contributor

Re: grep command

Hi (again):

Oh, and to show the linenumber on which the file matched, add the '-n' switch to your command"

# grep -n -e USERNAME -e EMAIL -e PHONENUMBER

Regards!

...JRF...
Jeff_Traigle
Honored Contributor

Re: grep command

Ah ok. egrep or grep -E is the way to go then. But the loop to prepend the numbers will be the same regardless.
--
Jeff Traigle
Sandman!
Honored Contributor

Re: grep command

>>How do I number the lines followed by a bracket?

Try this piplined construct...

# grep -v '^ADDRESS' input_file | awk '{print NR")"$0}'

hope it helps!
Unix or Linux?
Frequent Advisor

Re: grep command

Hi James

Thank you for your help, but I dont need the line number that it matches. I just simply want to display a number for each line followed by a bracket.

For example: with -n the match might be at line 55 but I would need to display the number of the line corresponding to the output screen
James R. Ferguson
Acclaimed Contributor

Re: grep command

Hi (again):

If I understand correctly, you want to number the lines of your screen output. You can do:

# grep -e USERNAME -e EMAIL -e PHONENUMBER filename | cat -n | more

Regards!

...JRF...
Tiziano Contorno _
Valued Contributor

Re: grep command

Create a file with this content (ie: awk_script):

BEGIN {LINE=1}
!/ADDRESS/ {print LINE")" $0 ; LINE++}

then:

awk -f awk_script your_file

Regards.
Unix or Linux?
Frequent Advisor

Re: grep command

Thank you