how can I write this shell script in python?

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

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.