Unix Programming - shell scripting with bash

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > shell scripting with bash





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author shell scripting with bash
Jonathan Taub

2004-04-28, 8:35 am

Hello there.

I'm quite new to unix/linux OSs.
I've got a task which is to look through a file for a line containing
a specific word and erase it if it exists. After that, I need to
insert a new line.

Can this be peformed by a *.sh file using bash?
If it not to much trouble, please provide such a script and links to a
good web site on such a subject.
Måns Rullgård

2004-04-28, 8:35 am

yonatan@il.nlayers.com (Jonathan Taub) writes:

> Hello there.
>
> I'm quite new to unix/linux OSs.
> I've got a task which is to look through a file for a line containing
> a specific word and erase it if it exists. After that, I need to
> insert a new line.
>
> Can this be peformed by a *.sh file using bash?


It can, but sed or PERL would seem like better tools for the job.

> If it not to much trouble, please provide such a script


This looks suspiciously like a homework assignment. If that is the
case you are supposed to do it yourself, but asking for help about
specific details is certainly OK. If you are doing this for some
other reason, someone will probably be able to give you some help.

> and links to a good web site on such a subject.


Sorry, I can't think of any at the moment.

--
Måns Rullgård
mru@kth.se
novalidname

2004-04-28, 1:35 pm

> Hello there.
>
> I'm quite new to unix/linux OSs.
> I've got a task which is to look through a file for a line containing
> a specific word and erase it if it exists. After that, I need to
> insert a new line.
>
> Can this be peformed by a *.sh file using bash?
> If it not to much trouble, please provide such a script and links to a
> good web site on such a subject.


Dont think many people will write that script for you :-)

Anyway what you could use from bash or your shell script is
`sed' which is a stream editor.
the command `man sed' will give you more info.

Question:
> look through a file for a line containing a specific word and
> erase it if it exists.


For example with sed..
sed 's/WORD_TO_LOOK_FOR/REPLACEMENT/g' input_file

Other examples; find and replace "foo" with "bar" on each line:
sed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
sed 's/\(.*\)foo/\1bar/' # replace only the last case

substitute "foo" with "bar" EXCEPT for lines which contain "baz"
sed '/baz/!s/foo/bar/g'

change "scarlet" or "ruby" or "puce" to "red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
gsed 's/scarlet\|ruby\|puce/red/g' # GNU sed only

If you use sed I advice you to take a look and study regular
expressions and a program called `grep', `man grep' for info.

possible links on the web.

Unix text processing, which is now an open book..
http://www.oreilly.com/openbook/utp/
linux documentation project, look for the bash guide...
http://www.tldp.org/
sed & awk code fragments
http://www.xs4all.nl/~winnetou/document/reminder.txt

And many, many other links at the www.. Search and you will find.

G00dlUck.. & Regards.
J.R.


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com