Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The script should accept three command-line arguments: the name of an input file, a word to search for, and the name of an output file.
The script should search the specified input file for the specified word, and put the search results into the specified output file.
For example, if I call the script like this:
FileName content.sh read outputFile.txt
... the script should search for the word read in the file called content.sh, and put the search results into the file called outputFile.txt
The script should accept three command-line arguments: the name of an input file, a word to search for, and the name of an output file.
The script should search the specified input file for the specified word, and put the search results into the specified output file.
For example, if I call the script like this:
FileName content.sh read outputFile.txt
... the script should search for the word read in the file called content.sh, and put the search results into the file called outputFile.txt
Okay...and what have you come up so far? Show us your code, or explain where you are stuck, with specific details. Otherwise, it sounds like you are asking us to do your homework for you.
Some tips:
read the bash man page ("man bash")
read up on bash position parameters ($0, $1, etc.)
you can use either the grep or awk GNU command to search
in bash, you can use the ">" character to redirect program output to a file
">>" appends to a file
if test $# -ne 3 #Error check
then
echo "you must provide 3 inputs. Good Bye"
exit
fi
grep -c "$2" $1 > $3
so far i did this.its coming out to be alright.but wanted to make sure whether it is completely right or not.
well, that's a pretty good start. the main thing is that the "-c" will tell grep to not print the search string it found, but rather a count. you said you wanted to print what you found, so leave that off.
if you want to literally only print the search string itself, and not the entire line in which the search string is found, then you can use the "-o" grep option.
actually, as you have a space in between the dash and the "o", it grep will look for a "-" in a file called "o", as well as in $2 and $1. The -o is a parameter, or option, and should never have a space after the dash. this is true of pretty much all commands that take options. take a look at the grep man page ("man grep") and you can see each the parameters listed there.
you can also do "grep --help" for a quick list of options (but don't do "grep -- help"!)
Yeah, with pretty much any GNU program, and really most UNIX/Linux commands, you can run "--help" (or "-h" or "--usage") to get some helpful output quickly printed to the screen.
so between doing "man <command>" and "<command> --help", you're well on your way!
it should also be mentioned that there is the "info" command, which is similar to man pages, but often times has more details. there seems to be less info pages, but some commands only have info pages and not man ones, so you can always try "info <command>", too.
one final note: many packages install documentation to whatever the standard doc dir is on your system. often times, that is /usr/share/doc. so it is worth your while to take a peek there, too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.