user3491122
user3491122

Reputation: 13

sed editing a line

I'm trying to use sed to modify a line like below

CustomLog /home/logs/httpd/access_log common

to

CustomLog "|/usr/sbin/rotatelogs /home/logs/httpd/access_log_%m_%d_%Y 86400" combined

I tried sed replace with text and it didn't work. I'm trying to do this for around 800 files, so need a single command to do this.

Upvotes: 1

Views: 67

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

sed -i.bak '/CustomLog/{
        s# # "|/usr/sbin/rotatelogs #
        s/ common$/_%m_%d_%Y 86400" combined/
}' *.conf

Upvotes: 0

Christian Fritz
Christian Fritz

Reputation: 21364

This seems to work:

sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\)/\1 "|\/usr\/sbin\/rotatelogs \2_`date +%m_%d_%Y` 86400" combined/'

Example:

> echo "CustomLog /home/logs/httpd/access_log common" | sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\)/\1 "|\/usr\/sbin\/rotatelogs \2_`date +%m_%d_%Y` 86400" combined/'

Upvotes: 1

Related Questions