Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Sed and Awk question.....

Status
Not open for further replies.

cmfwork

Technical User
Aug 18, 2000
5
0
0
US
How do I write to a file using sed and/ or awk.
My problem. I have a file that needs to be updated after and install. I want to script into the installation script to get the input from the user and plop that into the config file.
I can get the line number that I want to change by doing a sed command (sed -n -e /foo/= conf.file > linenum.file) and then reading the line num into a variable. I just don't know how to write the data that I need to write on that line number.

This is my test script so far:
echo "enter some text to write to file here:"
read enteredtext
sed -n -e /foo/= conf.file > linenum.file
read lineNum <linenum.file
# Now I have no clue as to what to do...
# I know that I have to stick foo at the beginning of the
# line that I am inserting (foo <entered text>)
any help? Idea? Am I doing things completely wrong... I know there has to be a better way to accomplish what I am doing
Thanks
-cmf
 
cmf,

Try this:

[tt] awk '{ print $0 } /foo/ { print &quot;newstuff&quot; }' < conf.file > conf.file.new[/tt]

The first {paragraph} is executed for all lines, simply printing that line.

The second {paragraph} is also executed for any line containing the matching string, and inserts a new line 'newstuff' into the output stream.

After that you would of course move conf.file.new into the place of the original file.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top