Help with grep (reWriting it in another way.)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grep (reWriting it in another way.)
# 1  
Old 12-16-2009
Help with grep (reWriting it in another way.)

Hello everyone!! Nice do be apart of your forum. I am not very good at unix and thats why i need your help i have this project where i have to simulate a grep command without using grep thought. I have to simulate

Code:
grep [-n] <parameters> <file>
grep [-v] <parameters> <file>
grep [-y] <parameters> <file>
grep [-i] <parameters> <file>
grep [-w] <parameters> <file>
grep [-f file] <parameters> <file>

as a file i am using /etc/passwd

Moreover they will have to be able to use paremeters such as

re+
re?
re1|re2

where you find in an egrep.
i know the first 2 can be done with sed but i am not sure if the re+,re? and re1|re2 parameters work...

Please if someone clould help i would be gratefull.

Thanks a lot.

*ps forgot to mention the language we use in script writing is awk.

Last edited by Franklin52; 12-16-2009 at 04:25 AM.. Reason: Please use code tags!
# 2  
Old 12-16-2009
Quote:
Originally Posted by kenshin88
*ps forgot to mention the language we use in script writing is awk.
Can you show us how your awk code looks like?
# 3  
Old 12-16-2009
Sure but i have olny made a menu to use these commands for simulation
This is the main file

Code:
#!/bin/sh
echo "

----------------------------------------------------------------------
"
while true;do
echo -n "           << Grep Simulation Script >>

----------------------------------------------------------------------

                   1.(n) Grep -n
                   2.(v) Grep -v
                   3.(y) Grep -y
                   4.(i) Grep -i 
                   5.(w) Grep -w
                   6.(f) Grep -f
                   7. (E)xit

             Doste epilogi gia prosomoiosi:   (Give your choise for sim)"
read epilogi
case $epilogi in
1|n|N)./grep-n;;
2|v|V)./grep-v;;
3|y|Y)./grep-y;;
4|i|I)./grep-i;;
5|w|W)./grep-w;;
6|f|F)./grep-f;;
7|e|E) exit 0;; 
*)echo " 

       !!!<< Dosate lathos epilogi, dokimaste ksana(wrong choise, try again, >>!!!
----------------------------------------------------------------------
";;
esac
done

The echos are in greek Smilie



The olny thing now is i will have to put 6 more files for the grep commands. As i said though they will have to be written differently and accept the above parameters.

Last edited by Franklin52; 12-16-2009 at 04:26 AM.. Reason: Please use code tags!
# 4  
Old 12-16-2009
You can do all those things with sed but they all need to be escaped with a "\" or use sed -r for extended regular expressions.

---------- Post updated at 09:26 ---------- Previous update was at 09:24 ----------

Oops.. you asked about sed but later you say the language has to be awk....
# 5  
Old 12-16-2009
Quote:
Originally Posted by Scrutinizer
You can do all those things with sed but they all need to be escaped with a "\" or use sed -r for extended regular expressions.

---------- Post updated at 09:26 ---------- Previous update was at 09:24 ----------

Oops.. you asked about sed but later you say the language has to be awk....
We are allowed to use sed to :P

Can you give me one exaple so i can convert the others

for instance i know

Code:
grep `example` file 

in sed is : 

sed -n `/example/p` file

question ... where do i put -r for extended regular expressions, next to the -n?
# 6  
Old 12-16-2009
Quote:
Originally Posted by kenshin88
We are allowed to use sed to :P

Can you give me one exaple so i can convert the others

for instance i know

Code:
grep `example` file 

in sed is : 

sed -n `/example/p` file

question ... where do i put -r for extended regular expressions, next to the -n?
Yes, so it would be sed -rn but you have to check your man page is -r is supported, otherwise you have to use \ everywhere.
For examples have a look at another thread in which I just happened to have posted an example.
# 7  
Old 12-16-2009
Code:
gawk '
function menu(){
        printf "\n<< Grep Simulation Script >>\n"
        print "------------------------------"
        print "1.(n) Grep -n"
        print "2.(v) Grep -v"
        print "3.(i) Grep -i"
        print "4.(w) Grep -w"
        print "5.(f) Grep -f"
        print "6. (E)xit"

}
function exists(file,      dummy, ret) {
   ret=0;
   if ( (getline dummy < file) >=0 ) {ret = 1; close(file)};
   return ret;
}

function grep(filename,text,linenum,invert,whole ){
   if ( exists(filename) ){
        linecount=0
        while ( (getline line < filename ) > 0 ) {
            linecount++
            if ( invert && line !~ text){
                print line
            }else if ( whole ){
                z=split(line, L, " ")
                for(j=1;j<=z;j++){
                    gsub(/^ +| +$/,"",L[i])
                    if ( L[j] == text ){
                        print line
                    }
                }
            } else if ( line ~ text) {
                if ( linenum )
                    print linecount": "line
                else { print line }
            }
        }
   }else{
        print "File: "filename" does not exists"
        return 1
   }

}
BEGIN{
    while(1){
        IGNORECASE=0
        linenum=0
        invert=0
        menu()
        printf "Your choice: "
        getline choice <"-"
        if ( choice == 1 ){
            linenum=1
        }else if ( choice == 2){
            invert=1
        }else if ( choice == 3 ){
            IGNORECASE=1
        }else if ( choice == 4){
            whole=1
        }else if ( choice == 5) {
            print "Not implemented"
            continue
        }else if ( choice == 6) {
            print "Exiting...."
            exit
        }
        printf "Enter text to search: "
        getline text <"-"
        printf "Enter filename: "
        getline file < "-"
        grep(file,text,linenum,invert,whole)
    }
}'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rewriting GNU uniq in awk

Within a shell script I use uniq -w 16 -D in order to process all lines in which the first 16 characters are duplicated. Now I want to also run that script on a BSD based system where the included version of uniq does not support the -w (--check-chars) option. To get around this I have... (7 Replies)
Discussion started by: mij
7 Replies

2. IP Networking

Transparent Proxy with URL Rewriting

All traffic on the LAN is routed through a single machine and filtered using iptables. I'd like to redirect this traffic to a transparent proxy running on the same machine that will rewrite the URL if it matches a specified regex, in which case the user will be redirected to a local server. In... (0 Replies)
Discussion started by: crottyan
0 Replies

3. UNIX for Dummies Questions & Answers

Rewriting line with tabs creating problem

Hi I have an input file which have random file in between.I have to manipulate each line and replace the character from position 5-10 with XXXXXX. But when I am writing this to on output file the tabs in between gets converted to normal space. Input file : 14207531131040896334R108 ... (4 Replies)
Discussion started by: akashtcs
4 Replies

4. UNIX and Linux Applications

sendmail recipient address rewriting with local_relay or mail_hub

I'm trying to make this work with a variety of different sendmail versions (all 8.8 or better) and different OS's. I have to configure all my unix workstations to deliver all email through a relay 'smtp.mydomain.com'. This includes unqualified names as well as qualified names and remote... (1 Reply)
Discussion started by: icefalcon
1 Replies

5. UNIX for Advanced & Expert Users

postfix sender address rewriting

Hi, I have a postfix server that relays to an exchange server. All of my unix/linux systems send to this server, the problem is the form the mail is sent with, the sender address is username@hostname.domain.local I need to rewrite every sender address to unix@maildomain.com for... (0 Replies)
Discussion started by: funksen
0 Replies

6. Shell Programming and Scripting

Rewriting standard output lines

Hello I'm curious about how to get a bash script to rewrite a line of standard output. For example, many programs track their progress by writing percentages on the screen: Precent Done: 60% That line gets updated periodically to reflect the status.. My question, is how do we do this, as... (5 Replies)
Discussion started by: neked
5 Replies

7. Shell Programming and Scripting

Help rewriting my KSH-script...

Hi! I'm a beginner at scripting, but have managed to complete a working KSH-script.. :D But since this is going to production i a few weeks, I would like to optimize it and make it better structured! The idéa of the script is to collect data from the database, put it in a file, and then... (2 Replies)
Discussion started by: linsto
2 Replies

8. UNIX for Dummies Questions & Answers

Rewriting a word from location

I am using: ..to get the word that is being searched. What I am looking to do, is to rewrite the word and us it in css: Sort of like this javascript: Hopefully I am making myself clear. Any ideas how I could do this? (1 Reply)
Discussion started by: marringi
1 Replies

9. IP Networking

Squid Proxy URL rewriting

I have an architecture as below <> <> There is a program in the AIX server which sends SMS to the internet, by sending HTTP request to the SMS processing server. Like, http://smsserver/mysms=test However the application does not have an option to specify where the Proxy server... (1 Reply)
Discussion started by: firdousamir
1 Replies
Login or Register to Ask a Question