Sponsored Content
Top Forums Shell Programming and Scripting Help with grep (reWriting it in another way.) Post 302380706 by ichigo on Wednesday 16th of December 2009 04:27:23 AM
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)
    }
}'

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for a pattern SYNOPSIS
grep [ option ... ] pattern [ file ... ] DESCRIPTION
Grep searches the input files (standard input default) for lines (with newlines excluded) that match the pattern, a regular expression as defined in regexp(6). Normally, each line matching the pattern is `selected', and each selected line is copied to the standard output. The options are -c Print only a count of matching lines. -h Do not print file name tags (headers) with output lines. -i Ignore alphabetic case distinctions. The implementation folds into lower case all letters in the pattern and input before interpre- tation. Matched lines are printed in their original form. -l (ell) Print the names of files with selected lines; don't print the lines. -L Print the names of files with no selected lines; the converse of -l. -n Mark each printed line with its line number counted in its file. -s Produce no output, but return status. -v Reverse: print lines that do not match the pattern. Output lines are tagged by file name when there is more than one input file. (To force this tagging, include /dev/null as a file name argument.) Care should be taken when using the shell metacharacters $*[^|()= and newline in pattern; it is safest to enclose the entire expression in single quotes '...'. SOURCE
/sys/src/cmd/grep.c SEE ALSO
ed(1), awk(1), sed(1), sam(1), regexp(6) DIAGNOSTICS
Exit status is null if any lines are selected, or non-null when no lines are selected or an error occurs. GREP(1)
All times are GMT -4. The time now is 02:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy