Linux shell scripts get and replace specific content in files

Source: Internet
Author: User

1. Get specific information from a string of strings

Requirement 1: Get the IP profile of the native IP:menu.lst as the system mirror, and need to obtain the native IP information from the file (get information from the files)

1Timeout12 default 03 4 Title Live5Find--Set-root/casper/Vmlinuz6Kernel/casper/vmlinuz boot=casper ignore_uuid showmounts Ip=eth0,10.0.66.66,255.255.240.0,10.0.64.37Initrd/casper/initrd.lz
Menu.lst

Requirement 2: Modify (replace) a specific content in the original file: Centerserver.conf is the configuration file for the SCC executable, and the domain value in the configuration file needs to be changed to native IP.

1 #CenterServer config file2 3# Logging Type:syslog|cerr|cout|file4Loggingtype =cout5 6# Logging level:none| Crit| err| warning|info| Debug|STACK7LogLevel =DEBUG8#LogLevel =INFO9 Ten #sip Listen Port OneUdpport =5060 A #SCC Domain IP -Domain =10.0.73.14 -Calltimeout = - the  -#RDS Client working thread num (1--8) -Rdsclientthreadnum =2 -#RDS Client Log on|if +Rdsclientlog =off -  +#Daemon Process Yes|no,default=Yes AIsdaemon =No at  - #Web Server Addr -Webserverurl = http://localhost:9000 -  - #SCC State Database -DataBase =SCC inMysqlusername =Root -mysqluserpasswd [email protected]666 to  +MsIp =127.0.0.1 -Msport =9999
centerserver.conf

Implementation script:

1#!/bin/SH2menulst="/jonet_boot/menu.lst"3Scc_path="/jonet/bin/scc/centerserver.config"4localip="' awk-f ', ' {print $ {} ' $MENULST |grep. '";5 Echo $LOCALIP6 7 MODIFY_SCC ()8 {9domain="' cat $SCC _path|grep Domain '"Ten Echo $DOMAIN One Echo $LOCALIP A#sed-E'S/domain = 10.0.66.66/domain = 10.0.73.15/g'$SCC _path -Sed-i's/' "${domain}"'/domain =' "${localip}"'/g'$SCC _path - } the  -  - modify_conf () - { + MODIFY_SCC - } +  A modify_conf at  -Exit0
conf.sh

Description: Sed-i ' s/' "${domain}" '/domain = ' ${localip} ' '/g ' $SCC _path

Because macros define DOMAIN and Localip are not available in commands using $domain and $localip, you need to use ' ${domain} ' and ' ${localip} ' to enable script substitution

1#!/bin/SH2menulst="/jonet_boot/menu.lst"3Scc_path="/jonet/bin/scc/centerserver.config"4localip="' awk-f ', ' {print $ {} ' $MENULST |grep. '";5 Echo $LOCALIP6 7 MODIFY_SCC ()8 {9domain="' cat $SCC _path|grep Domain '"Ten Echo $DOMAIN One Echo $LOCALIP ASed-i's/' "${domain}"'/domain =' "${localip}"'/g'$SCC _path - } -  the modify_conf () - { - MODIFY_SCC - } +  - modify_conf +  AExit0
conf.sh
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.