Linux Text processing tools sed exercises

Source: Internet
Author: User

1. Use the SED command to print out the IP address of the ifconfig ens33

Solution: (1) ifconfig Ens33 | Sed-n ' 2p ' | Sed ' s/.*inet//' | Sed ' s/netmask.*//'

(2) Ifconfig Ens33 | Sed-n ' 2p ' | Sed-r ' s/.*inet (. *) netmask.*/\1/'

(3) Ifconfig Ens33 | Sed-r-N ' 2s/.*inet (. *) netmask.*/\1/p '


2. Use the SED command to print out the system version

Solution: Sed-r-N ' s/.*release[[:space:]]* ([^.] +). */\1/p '/etc/centos-release


3, script title: Use SED to determine the version number if the version number is 7 in/etc/default/grub grub_cmdlinux_linux= "RHGB quiet" after the addition of xxxxx characters, if not exit does not execute

Solution: vim/root/bin/test/sedtest.sh

#!/bin/bash

banben= ' Sed-nr ' s/.*release[[:space:]]* ([^.] +). */\1/p '/etc/centos-release '

["$banben"-eq 7] && Sed-nr-i.bak ' s/(. *linux= ". *)"/\1 xxxxxxxx "/P '/etc/default/grub | | Exit 20

The additional section can also be used in this way: Sed-r ' s/quiet/& xxxx/'/etc/default/grub (the & symbol in the sentence represents all the preceding characters)


4. Use SED to remove the base name and directory name from the/etc/sysconfig/network-scripts/

Solution: echo/etc/sysconfig/network-scripts/| Sed-nr ' s#.*/(. +)/?#\1#p ' Take the base name

echo/etc/sysconfig/network-scripts/| Sed-nr ' s# (. *) (. +)/?#\1#p ' Fetch folder name

echo/etc/sysconfig/network-scripts/| Sed-nr ' s# (. *) (. +)/?#\2#p ' Take the base name


5, using SED will/etc/httpd/conf/httpd.conf #namevirtualhost *:80 Remove the # number, while the last 7 lines of # are removed

Solution: Sed-e ' s/#Name/name/'-e '/#<virtualhost/,/#<\/virtual/s/#//'/etc/httpd/conf/httpd.conf


Linux Text processing tools sed exercises

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.