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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help replacing a work in a file with a variable plus a word.

Status
Not open for further replies.

cden2

Technical User
Dec 29, 2009
3
US
I am a new to scripting. I am working with a script to replace the
word 'name' in a file with the name of the server. I am trying pass a
variable in a search and replace command using sed. It works now, but
only replaces the word 'name' with the '${SERVER}: name' and not the
actual hostname.

TMPDIR="/rae/tmp"
SERVER="`hostname`"

sed -i 's/name:/${SERVER}: name/g' $TMPDIR/mounts.$SERVER

Currently in file
------------------------

name: chris

Trying to make the output to the file mounts.$SERVER become:
-------------------------------------------------------------------------------------------

cltgap01: name: chris

Could awk or gawk work better for this? Please help.

Thanks,
Chris
 
PDreyer!!! Thanks for your help it works. I am in the final stage of the script.

Question 1:

The output is created in a file called reportmounts which is appended with the DATE.TIME.

I want to email the latest DATE.TIME stamped file content view email to an address.

How do I script this?

directory: /rae/tmp contains

reportmounts.091230.0825
reportmounts.091230.0830
reportmounts.091230.0840
reportmounts.091230.0950

I want the last reportmouts file to be emailed.

Chris
 
Hi

Code:
[navy]lastfile[/navy][teal]=[/teal][green][i]"$( ls -1tr /rae/tmp/reportmounts.*.* | head -1 )"[/i][/green]

[gray]# send in the mail body[/gray]
mail -s [green][i]'Latests mount report'[/i][/green] [green][i]'someone@example.com'[/i][/green] [teal]<[/teal] [green][i]"$lastfile"[/i][/green]

[gray]# send as attachment[/gray]
mutt -s [green][i]'Latests mount report'[/i][/green] -a [green][i]"$lastfile"[/i][/green] [green][i]'someone@example.com'[/i][/green] [teal]<<[/teal]ENDOFTEXT
Hi master[teal],[/teal]

Here I send you attached the latest mount report[teal],[/teal]
[navy]$lastfile[/navy]

Sincerely[teal],[/teal]
Your operating system
ENDOFTEXT

Feherke.
 
Thanks!!! that worked. It brought about an issue.

lastfile="$( ls -1tr /rae/tmp/reportmounts.*.* | head -1 )"

works, but if it is the first file being put in the directory, it says no $lastfile exist. So I am trying to create an if statement that will check else just use the existing file.

2)I also want to put a statement at the end of the script to have it run every 5 minutes. The output file will pile up, so I am going to try and script it to delete the prior day mounts. The files should not be that large.

Let me know if you can help.

THanks,
Chris



 
Hi

Chris said:
works, but if it is the first file being put in the directory, it says no $lastfile exist.
I do not understand this. You are running that code after a reportmounts file was created, right ? ( By the way, how is that created ? ) So there must be at least one. So there is no reason for that code to complain.
Chris said:
I also want to put a statement at the end of the script to have it run every 5 minutes.
And also send mail every 5 minutes ? That is far too frequent. Maybe you should rethink the whole idea. Anyway, to repeat tasks use [tt]cron[/tt].
Chris said:
The output file will pile up, so I am going to try and script it to delete the prior day mounts.
That is simple and frequent. We already have dozens of threads about deleting files older than whatever. Please search this forum's old threads.

Feherke.
 
Chris,

Try using lastfile="$( ls -1tr /rae/tmp/reportmounts.*.* | tail -1 )" to get the latest file to email.

And if this is Solaris you can use either mailx or uuencode to either mail or attach the resultant file.

As Feherke mentioned though, doing this every five minutes is a little overkill.
Adding a command to delete older verisons has been heavily documented on this site.

TIP: find, xargs, exec

Hope this helps
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top