egrep output order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep output order
# 1  
Old 07-25-2012
egrep output order

The order of egrep output seems to be as they occur in the file. How do I get the order as requested? For e.g.


file contents:
Code:
AAA
EEE
GGG

egrep 'GGG|AAA|EEE' file

gives
Code:
    AAA
    EEE
    GGG

instead of

Code:
    GGG
    AAA
    EEE


Last edited by Franklin52; 07-25-2012 at 04:09 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-25-2012
grep doesn't remember past output, checks lines one at a time against the regex given.

Your request seems a bit ambiguous. What order would you want if you were given a file like this?

Code:
AAA
eee
GGG
eee
AAA

# 3  
Old 07-25-2012
You'd have to run egrep three times and make three passes through the file:

Code:
egrep 'GGG' file
egrep 'AAA' file
egrep 'EEE' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Egrep output

If I have an output from egrep that has two elements on separate lines can I concatenate them? so the egrep for examople might be: egrep "filename|type" And the actual output might be: ./file001.doc Type Text Document How do I get the egrep to remove the line feed so the output is: ... (5 Replies)
Discussion started by: TuftyDave
5 Replies

2. Shell Programming and Scripting

awk - Why does output order change?

I have this END section in an awk script: END { for (j in littlebin) { print (j, int(log(j)/log(2)) , littlebin) lbsum+=littlebin } for (i in bins) ... (6 Replies)
Discussion started by: treesloth
6 Replies

3. Shell Programming and Scripting

How to print the output in correct order?

Hi, while using following awk commend I’m getting confused, The output is not like as the row present in input files, can anyone explain and tell me how to print in the order like in input. value=$(awk 'FNR>1 && NR==FNR{a=$4;next} a{sum+=$4} END {for(i in sum){printf i"\t"sum/2"@@";}}'... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

4. Shell Programming and Scripting

Using egrep to output expressions which are not found

Hi, Im using the below command to search a file for multiple expressions if the 4th expression doesnt exist the command simply lists what it can find and ignores what it cant. Is there any way to get the command to output an error or a message if it cant find the 4th expression to a file? ... (16 Replies)
Discussion started by: 02JayJay02
16 Replies

5. Shell Programming and Scripting

egrep command and order

Hi All, Here is my question. I have two files, file1.txt and file2.txt. I need the line number (index number) of file2.txt where the words in file1.txt appear. But they have to be in the same order as file1.txt. In example, file1.txt Z K A ... T file2.txt W A Q R (6 Replies)
Discussion started by: senayasma
6 Replies

6. Shell Programming and Scripting

egrep output each file in new line

I have the following script that searches in several files and shows the search results and the matches filename on the screen. find . -exec egrep -wH "word1|word2" {} \; the output from the search display as: file1 word1 word2 I need to show each file search output result on new... (5 Replies)
Discussion started by: konddor
5 Replies

7. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

8. Shell Programming and Scripting

adding order number in the output file

Hello, I have a list of all order number like below: maryu01001 johnm00003 peter02234 catmy66751 For the above, all number must in 5 digits and I need to add one into the number and output a new list like below: maryu01002 johnm00004 peter02235 catmy66752 How can write a... (20 Replies)
Discussion started by: happyv
20 Replies

9. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies

10. Shell Programming and Scripting

Comma Delimited Output w/ egrep

Hi all, I have an input file that I am pulling out certain phases using the following commands: cat /nodes.txt | egrep -e 'OSVersion|PrimaryNodeName' Currently the output looks like this: OSVersion - 5.0 PrimaryNodeName - serverA OSVersion - 5.0 PrimaryNodeName - serverB OSVersion... (2 Replies)
Discussion started by: indianadoug
2 Replies
Login or Register to Ask a Question