How to write a shell script using sed to re-implement the functionality of egrep?
-
Write a script which re-implements the functionality of egrep. Like egrep, it must read from files passed as arguments, and if there weren't any, read instead from standard input (try /dev/stdin). Use sed to check whether the pattern matches a line — do not use grep or egrep in your program. The only option the script must support is -n (number the output lines).
-
Answer:
4 possible cases - egrep_script pattern - egrep_script options pattern - egrep_script pattern file - egrep_script options pattern file if [ $# -eq 1 ] # do the stdin stuff (forever loop) elif [ $# -eq 2 ] # for two arguments ... if [ $1 = "-n" ] # do the "options pattern" stdin stuff ... else # do the "pattern file" stuff ... fi elif [ $# -eq 3 ] # do the options pattern file stuff ... if [ -e "$3" ] # check if the user-inputted file exists or not and do the searching stuff ... else ... ... echo "$3 is non-existent" ... fi else # bad number of arguments, ie 4 or more, error output to user here
alvinli2... at Yahoo! Answers Visit the source
Related Q & A:
- How To Write A Resume?Best solution by Yahoo! Answers
- How to deploy a server script?Best solution by Stack Overflow
- How to execute Unix shell script from Windows?Best solution by Stack Overflow
- How to write Windows registry script?Best solution by forums.codeguru.com
- How to write a persuasive letter to a bank?Best solution by wikihow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.