LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-10-2016, 07:48 AM   #1
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Rep: Reputation: 0
Adding a string to the start of a line in linux


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:

/var/spool/mail/root /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'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.
 
Old 11-10-2016, 08:17 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,076

Rep: Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378
Quote:
Originally Posted by casperpache View Post
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:

/var/spool/mail/root /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'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".

Last edited by TB0ne; 11-10-2016 at 08:19 AM.
 
1 members found this post helpful.
Old 11-10-2016, 08:36 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
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.

To install on centos 7:
Code:
yum install python-pip python-devel
pip install fabric
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
 
2 members found this post helpful.
Old 11-10-2016, 08:39 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,363
Blog Entries: 3

Rep: Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045
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.
 
2 members found this post helpful.
Old 11-10-2016, 08:46 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,076

Rep: Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378Reputation: 4378
Quote:
Originally Posted by Turbocapitalist View Post
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...
 
Old 11-10-2016, 09:10 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,363
Blog Entries: 3

Rep: Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045Reputation: 1045
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 11-10-2016, 09:39 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
Nice. I always forget about the delimiter option in sed.

If we are concentrating on making a more readable sed statement rather than how to apply it to multiple server, my submission is:

Code:
# insert /var/log/messages into beginning of first line
# using ; as a delimiter as suggested earlier
sed -i '1s;^;/var/log/messages ;' /etc/syslog
 
1 members found this post helpful.
Old 11-10-2016, 10:06 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Not sure if we can assume inserting on the first line (up to OP of course), but a slightly shorter sed would be:
Code:
sed 's;^/var/log/messages;/var/spool/mail/root &;' /etc/syslog
 
2 members found this post helpful.
Old 11-10-2016, 10:46 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
It looks like more of a logrotate than a syslog config file to me
 
Old 12-13-2016, 03:53 AM   #10
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array guru11 Programming 5 12-29-2011 09:34 AM
adding number and string to each line in a data file teresevo1 Programming 7 04-11-2010 12:03 AM
Adding line of text to start of file roadrash Linux - General 2 10-26-2009 07:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration