shell编程系列13--文本处理三剑客之sed利用sed追加文件内容

shell编程系列13--文本处理三剑客之sed利用sed追加文件内容

追加用法总结:
    1、a        在匹配行后面追加
    2、i        在匹配行前面追加
    3、r        将文件内容追加到匹配行后面
    4w        将匹配行写入指定文件

追加用法示例详解:
    1、a
        (1)、passwd文件第10行后面追加"Add Line Behind"
            sed -i '10aAdd Line Behind' passwd 
        (2)、passwd文件第10行到第20行,每一行后面都追加"Test Line Behind"
            sed -i '10,20a Test Line Behind' passwd
        (3)、passwd文件匹配到/bin/bash的行后面追加"Insert Line For /bin/bash Behind"
            sed -i '/\/bin\/bash/a Insert Line For /bin/bash Behind' passwd

    2、i
        (1)、passwd文件匹配到以nginx开头的行,在匹配行前面追加"Add Line Before"
            sed -i '/^nginx/i Add Line Before' passwd
        (2)、passwd文件每一行前面都追加"Insert Line Before Every Line"
            sed -i 'a Insert Line Before Every Line' passwd

    3、r
        (1)、将/etc/fstab文件的内容追加到passwd文件第20行后面
            sed -i '20r /etc/fstab' passwd
        (2)、将/etc/inittab文件内容追加到passwd文件匹配到/bin/bash行的后面
            sed -i '/\/bin\/bash/r /etc/inittab' passwd
        (3)、将/etc/vconsole.conf文件内容追加到passwd文件中特定行后面,匹配以ftp开头的行,到第18行的所有行
            sed -i '/^ftp/,18r /etc/vconsole.conf' passwd

    4w
        (1)、将passwd文件匹配到/bin/bash的行追加到/tmp/sed.txt文件中
            sed -i '/\/bin\/bash/w /tmp/sed.txt' passwd
        (2)、将passwd文件从第10行开始,到匹配到/sbin/nologin的所有行内容追加到/tmp/sed-1.txt
            sed -i '10,/\/sbin\/nologin/w /tmp/sed-1.txt' passwd

# 大写字母替换为小写字母

混合区间匹配读取内容追加容易出错
在处理几十万上百万的文件中,可以找出特定的行,输出到一个文件中,然后再对这个文件进行处理

 

具体示例

需求:公司的购买页面添加了跟踪参数的配置,谷歌浏览器升级后点击页面没法把参数带到购买页面,没法跟踪来源

需要在域名的nginx配置文件中添加 add_header Referrer-Policy "no-referrer-when-downgrade" always;

如下图:需要referer部分带上参数

 

1.批量添加 add_header Referrer-Policy "no-referrer-when-downgrade" always;  头文件的脚本

# vim /data/vhost_nginx.sh

#!/bin/bash
# 
# 域名列表
domains="chinasoft.com
imusic.chinasoft.com
itube.chinasoft.com"

# 配置文件的路径
config_dst="/usr/local/nginx/conf/vhost.d/"
suffix=".conf"
# 找出域名中没有添加 add_header Referrer-Policy "no-referrer-when-downgrade" always; 头的配置,并在 error_page 后面加上
for domain in ${domains};do
if [ -f /usr/local/nginx/conf/vhost.d/${domain}.conf ];then

# 判断是否已经配置,避免参数设置重复
count=`grep -ri "no-referrer-when-downgrade" ${config_dst}${domain}${suffix}|wc -l`
#count=`grep -ri "error_page" ${config_dst}${domain}${suffix}|wc -l`
if [[ ${count} -lt 1 ]];then
# 将 /data/domainpolicy.txt 中的内容追加到 error_page 这行的下面
sed -i '/error_page/r /data/domainpolicy.txt' ${config_dst}${domain}${suffix}
fi

fi
done

2.检测的脚本,检查是否配置上了参数
# vim /data/check_nginx.sh

#!/bin/bash
#
domains="chinasoft.com
imusic.chinasoft.com
itube.chinasoft.com"

config_dst="/usr/local/nginx/conf/vhost.d/"
suffix=".conf"
# 找出域名中没有添加 add_header Referrer-Policy "no-referrer-when-downgrade" always; 头的配置,并在 error_page 后面加上
for domain in ${domains};do
if [ -f /usr/local/nginx/conf/vhost.d/${domain}.conf ];then

count=`grep -ri "no-referrer-when-downgrade" ${config_dst}${domain}${suffix}|wc -l`
#count=`grep -ri "error_page" ${config_dst}${domain}${suffix}|wc -l`
if [[ ${count} -lt 2 ]];then
#sed -i '/error_page/r /data/domainpolicy.txt' ${config_dst}${domain}${suffix}
echo ${domain}
fi

fi
done
# cat /data/domainpolicy.txt 
    add_header Referrer-Policy "no-referrer-when-downgrade" always
最终的配置示例
server { listen
80; server_name miao.chinasoft.cn ori-miao.chinasoft.cn; access_log /data/www/logs/nginx_log/access/miao.chinasoft.cn_access.log main ; error_log /data/www/logs/nginx_log/error/miao.chinasoft.cn_error.log ; root /data/www/vhosts/miao.chinasoft.cn/httpdocs ; index index.html index.shtml index.php ; include rewrite.d/miao.chinasoft.cn.conf ; error_page 404 403 /404.html; add_header Referrer-Policy "no-referrer-when-downgrade" always; rewrite ^/(.*)$ https://miao.chinasoft.cn/$1 permanent; } server { listen 443 ssl; ssl_certificate cert2016/chinasoft_cn.crt; ssl_certificate_key cert2016/chinasoft_cn.key; ssl_dhparam cert2016/dh_2048.pem; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!AES128-GCM-SHA256:!AES256-GCM-SHA384:!AES128-SHA256:!AES256-SHA256:!AES128-SHA:!AES256-SHA:AES:!CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"; ssl_prefer_server_ciphers on; #ssl_stapling on; #ssl_stapling_verify on; add_header 'Access-Control-Allow-Origin' '*'; server_name miao.chinasoft.cn ori-miao.chinasoft.cn; access_log /data/www/logs/nginx_log/access/miao.chinasoft.cn_access.log main ; error_log /data/www/logs/nginx_log/error/miao.chinasoft.cn_error.log ; root /data/www/vhosts/miao.chinasoft.cn/httpdocs ; index index.html index.shtml index.php ; include rewrite.d/miao.chinasoft.cn.conf ; error_page 404 403 /404.html; add_header Referrer-Policy "no-referrer-when-downgrade" always; if ($http_user_agent ~ Ezooms) { return 403; } location ~ ^.*\.(htaccess|htpasswd|ini|sh)$ { deny all; } location ~ \.php$ { proxy_pass http://php_pool; expires -1; include proxy_params; } location / { include proxy_params; if (!-d $request_filename){ set $flag 1$flag; } if (!-f $request_filename){ set $flag 2$flag; } if ($flag = "21"){ proxy_pass http://php_pool; expires -1; } } }

 示例2:

需要在nginx添加lua相关的配置

1.lua脚本

# cat /usr/local/nginx/conf/lua/cookies/cookie.lua

if ngx.req.get_method() == "GET" then
ngx.exit(404)
end
 
ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if err == "truncated" then
-- one can choose to ignore or reject the current request here
end
 
if not args then
ngx.say("failed to get post args: ", err)
return
end
 
local cookie = ""
local expires = 3600 * 24 * 30 * 13  -- 13 months
for key, val in pairs(args) do
cookie = key .. "=" .. val .. "; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expires)
if type(ngx.header["Set-Cookie"]) == "table" then
    ngx.header["Set-Cookie"] = { cookie, unpack(ngx.header["Set-Cookie"]) }
else
    ngx.header["Set-Cookie"] = { cookie, ngx.header["Set-Cookie"] }
end
 
end
 
ngx.say(ngx.md5(ngx.var.remote_addr))

2.在域名中应用配置

location /cookie {
        default_type 'text/plain';
        content_by_lua_file '/usr/local/nginx/conf/lua/cookies/cookie.lua';
}

3.测试

curl --location --request POST 'https://域名/cookie' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'cj=123' --data-urlencode 'event=123456'

 # 批量修改nginx配置的脚本
 # cat vhost_nginx.sh

#!/bin/bash
# 
# 域名列表
domains="a.chinasoft.com
b.chinasoft.com
c.chinasoft.com"

# 配置文件的路径
config_dst="/usr/local/nginx/conf/vhost.d/"
suffix=".conf"
content="domain_cookie.txt"

# 找出域名中没有添加 cookie.lua; 的配置,并在 error_page 或者 no-referrer-when-downgrade后面加上
for domain in ${domains};do
    if [ -f /usr/local/nginx/conf/vhost.d/${domain}.conf ];then

        # 判断是否已经配置,避免设置重复
        count=`grep -ri "cookie.lua" ${config_dst}${domain}${suffix}|wc -l`
        if [[ ${count} -lt 1 ]];then
            count_ref=`grep -ri "no-referrer-when-downgrade" ${config_dst}${domain}${suffix}|wc -l`
            if [[ ${count_ref} -lt 1 ]];then
                # 将 /data/domain_cookie.txt 中的内容追加到 error_page 这行的下面
                sed -i '/error_page/r /data/domain_cookie.txt' ${config_dst}${domain}${suffix}
            fi
        fi

        if [[ ${count} -lt 1 ]];then
            count_ref02=`grep -ri "no-referrer-when-downgrade" ${config_dst}${domain}${suffix}|wc -l`
            if [[ ${count_ref} -gt 1 ]];then
                # 将 /data/domain_cookie.txt 中的内容追加到 error_page 这行的下面
                sed -i '/no-referrer-when-downgrade/r /data/domain_cookie.txt' ${config_dst}${domain}${suffix}
            fi
        fi

    fi
done

# cat /data/domain_cookie.txt

    location /cookie {
        default_type 'text/plain';
        content_by_lua_file '/usr/local/nginx/conf/lua/cookies/cookie.lua';
    }

# 检查配置是否插入成功脚本

# cat check_nginx.sh 
#!/bin/bash
#
domains="a.chinasoft.com
b.chinasoft.com
c.chinasoft.com"

config_dst="/usr/local/nginx/conf/vhost.d/"
suffix=".conf"
# 找出域名中没有添加 cookie.lua 配置
for domain in ${domains};do
    if [ -f /usr/local/nginx/conf/vhost.d/${domain}.conf ];then

        count=`grep -ri "cookie.lua" ${config_dst}${domain}${suffix}|wc -l`
        if [[ ${count} -lt 2 ]];then
            echo ${domain}
        fi

    fi
done

 

posted @ 2019-06-04 21:14  reblue520  阅读(859)  评论(0编辑  收藏  举报