[SOLVED] Adding a string to the start of a line in linux
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
I've searched for a way to do this but I cant find anything for this specific scenario.
It needs to be inserted on the 1st line because I believe that's the way the file is read by the logrotate.
Is anyone able to offer some advice please?
Thanks
Dave
Last edited by casperpache; 11-10-2016 at 07:49 AM.
Hi all,
Although im not really new to Linux I am still a learner to scripting. I simply want to add a new location to my /etc/syslog file but I cant figure out the command. So... I want to add /var/spool/root to my syslog file by using a script (because I want to run this script across multiple servers) to add it in.
My syslog is currently showing as:
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron /var/log/sulog {
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
I want it to include the location /var/spool/root as below:
I've searched for a way to do this but I cant find anything for this specific scenario. It needs to be inserted on the 1st line because I believe that's the way the file is read by the logrotate.
If you're familiar with Linux, you must have run across the 'sed' command before, and I'm fairly positive that any web searches you did would have turned it up as well. Also...what have you done/tried on your own so far???? Also...you've asked about scripting in 2011...FIVE YEARS AGO...have you not progressed any in that time?
A simple
Code:
sed -i 's/^\/var\/log\/messages/\/var\/spool\/mairoot \/var\/log\/messages/g'
...works very well. Any research on the sed command would show you the caret '^' indicates a beginning-of-line....so the search/replace looks for a beginning-of-line, followed by /var/log/messages, and replaces it with "/var/spool/mairoot /var/log/messages".
In case you need to run things on multiple hosts now or in the future I would suggest using python 'fabric' which makes it far easier to send commands.
A script 'runthis.py' to log in to a host and run the above sed command from tb0ne:
Code:
from fabric.api import *
def athost(ip):
env.user = 'root'
with settings(host_string='{0}'.format(ip)):
run('sed -i "s/^\/var\/log\/messages/\/var\/spool\/mairoot \/var\/log\/messages/g" /etc/syslog')
Command to run this on a host:
Code:
# prompts for password
fab -f runthis.py athost:ip=192.168.100.1
# includes password in command
fab -P somepassword -f runthis.py athost:ip=192.168.100.1
Yes, "sed" a good way to do it in a script. It's quite flexible. The suggestion above can be made more readable using a different delimiter for the substitution so that the slashes don't have to be escaped.
Code:
sed 's|^/var/log/messages|/var/spool/mailroot /var/log/messages/|' /etc/syslog
So instead of s/// you have s|||. If you prefer, you could use s### or just about anything else as long as they are three of a kind.
Yes, "sed" a good way to do it in a script. It's quite flexible. The suggestion above can be made more readable using a different delimiter for the substitution so that the slashes don't have to be escaped.
Code:
sed 's|^/var/log/messages|/var/spool/mailroot /var/log/messages/|' /etc/syslog
So instead of s/// you have s|||. If you prefer, you could use s### or just about anything else as long as they are three of a kind.
Nice one....I've always gone 'old-school' and just escaped things with backslashes. Sed has a HUGE amount of flexibility, though...
I've always gone 'old-school' and just escaped things with backslashes.
Me, too. Though one day I had some hideous pattern with a dozen or so slashes that had to be replaced with something similar. For me that was the turning point on that aspect of "sed". I admire how someone had not only written the code in C for "sed" but come up with the bare idea itself before that in sufficient detail to implement it. GNU "sed" is a re-implementation and so is the BSD version I have elsewhere. The farthest I can trace it online is sed on sourceforge, though there might be something in one of the printed books on the topic.
Guys, the response on this was awesome and I really appreciate the time you have taken to assist.
Just a few things...
yes, this is related to the logrotate, it is a new location that we would like to rotate on multiple servers.
My scripting knowledge is unfortunately not very progressive due to my workload of OS and hardware issues... I get the time to quickly provide answers/solutions, but afterwards, not enough time to digest the methods. (Cant believe its been 5 years since I posted the last scripting thread.)
From that, I realise that I really need to learn this via a proper structure to understand it.
SED looks great and although I have heard of it before, i've not really understood its capabilities.
Thanks again, ill mark this as complete as this has set me en-route to my solution..... hopefully my next scripting request will be at a more advanced level.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.