美文网首页
shell入门23-sed实践

shell入门23-sed实践

作者: 万州客 | 来源:发表于2023-01-05 09:36 被阅读0次

倒数第二章了。

一,理论


image.png
image.png
image.png
image.png
image.png

二,练习代码

[root@127 shell_demo]# sed 'p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -n 'p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -n '1p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
[root@127 shell_demo]# sed -n '2p' /etc/hosts
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@127 shell_demo]# df -h |sed -n '2p'
devtmpfs                 3.9G     0  3.9G   0% /dev
[root@127 shell_demo]# free |sed -n '2p'
Mem:        8172936      530844     1478844        8844     6163248     7329312
[root@127 shell_demo]# free
              total        used        free      shared  buff/cache   available
Mem:        8172936      530976     1478712        8844     6163248     7329180
Swap:       8257532           0     8257532
[root@127 shell_demo]# cat -n /etc/passwd > /tmp/passwd
[root@127 shell_demo]# sed -n '1, 3p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@127 shell_demo]# sed -n '1p; 3p; 6p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
[root@127 shell_demo]# sed -n '2p; 8p' /tmp/passwd 
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     8  halt:x:7:0:halt:/sbin:/sbin/halt
[root@127 shell_demo]# sed -n '3, 5p' /tmp/passwd 
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@127 shell_demo]# sed -n '4, $p' /tmp/passwd 
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
    11  games:x:12:100:games:/usr/games:/sbin/nologin
    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13  nobody:x:99:99:Nobody:/:/sbin/nologin
    14  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15  dbus:x:81:81:System message bus:/:/sbin/nologin
    16  polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    18  postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    20  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    21  redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
    22  mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
    23  saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    24  telegraf:x:996:993::/etc/telegraf:/bin/false
    25  apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    26  ntp:x:38:38::/etc/ntp:/sbin/nologin
    27  sky:x:1001:1001::/home/sky:/bin/bash
    28  nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed -n '3, +3p' /tmp/passwd 
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
[root@127 shell_demo]# sed -n '1~2p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    11  games:x:12:100:games:/usr/games:/sbin/nologin
    13  nobody:x:99:99:Nobody:/:/sbin/nologin
    15  dbus:x:81:81:System message bus:/:/sbin/nologin
    17  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    21  redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
    23  saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    25  apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    27  sky:x:1001:1001::/home/sky:/bin/bash
[root@127 shell_demo]# sed -n '2~2p' /tmp/passwd 
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     8  halt:x:7:0:halt:/sbin:/sbin/halt
    10  operator:x:11:0:operator:/root:/sbin/nologin
    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    14  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    16  polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    18  postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    20  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    22  mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
    24  telegraf:x:996:993::/etc/telegraf:/bin/false
    26  ntp:x:38:38::/etc/ntp:/sbin/nologin
    28  nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed -n '4~2p' /tmp/passwd 
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     8  halt:x:7:0:halt:/sbin:/sbin/halt
    10  operator:x:11:0:operator:/root:/sbin/nologin
    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    14  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    16  polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    18  postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    20  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    22  mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
    24  telegraf:x:996:993::/etc/telegraf:/bin/false
    26  ntp:x:38:38::/etc/ntp:/sbin/nologin
    28  nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed -n '3~2p' /tmp/passwd 
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    11  games:x:12:100:games:/usr/games:/sbin/nologin
    13  nobody:x:99:99:Nobody:/:/sbin/nologin
    15  dbus:x:81:81:System message bus:/:/sbin/nologin
    17  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    21  redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
    23  saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    25  apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    27  sky:x:1001:1001::/home/sky:/bin/bash
[root@127 shell_demo]# sed -n '3~4p' /tmp/passwd 
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    11  games:x:12:100:games:/usr/games:/sbin/nologin
    15  dbus:x:81:81:System message bus:/:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    23  saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    27  sky:x:1001:1001::/home/sky:/bin/bash
[root@127 shell_demo]# sed -n '4~5p' /tmp/passwd 
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    14  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    24  telegraf:x:996:993::/etc/telegraf:/bin/false
[root@127 shell_demo]# sed -n '$p' /tmp/passwd 
    28  nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed -n '/root/p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
    10  operator:x:11:0:operator:/root:/sbin/nologin
[root@127 shell_demo]# sed -n '/bash$/p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    22  mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
    27  sky:x:1001:1001::/home/sky:/bin/bash
[root@127 shell_demo]# sed -n '/s...:x/p' /tmp/passwd 
     6  sync:x:5:0:sync:/sbin:/bin/sync
    17  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@127 shell_demo]# sed -n '[0-9]/p' /tmp/passwd 
sed: -e expression #1, char 1: unknown command: `['
[root@127 shell_demo]# sed -n '/[0-9]/p' /tmp/passwd 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
    11  games:x:12:100:games:/usr/games:/sbin/nologin
    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13  nobody:x:99:99:Nobody:/:/sbin/nologin
    14  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15  dbus:x:81:81:System message bus:/:/sbin/nologin
    16  polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    18  postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    19  chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
    20  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    21  redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
    22  mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
    23  saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    24  telegraf:x:996:993::/etc/telegraf:/bin/false
    25  apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    26  ntp:x:38:38::/etc/ntp:/sbin/nologin
    27  sky:x:1001:1001::/home/sky:/bin/bash
    28  nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed -n '/^http/p' /tmp/passwd 
[root@127 shell_demo]# sed -n '/^http/p' /etc/services
http            80/tcp          www www-http    # WorldWideWeb HTTP
http            80/udp          www www-http    # HyperText Transfer Protocol
http            80/sctp                         # HyperText Transfer Protocol
https           443/tcp                         # http protocol over TLS/SSL
https           443/udp                         # http protocol over TLS/SSL
https           443/sctp                        # http protocol over TLS/SSL
http-mgmt       280/tcp                 # http-mgmt
http-mgmt       280/udp                 # http-mgmt
http-rpc-epmap  593/tcp                 # HTTP RPC Ep Map
http-rpc-epmap  593/udp                 # HTTP RPC Ep Map
httpx           4180/tcp                # HTTPX
httpx           4180/udp                # HTTPX
http-wmap       8990/tcp                # webmail HTTP service
http-wmap       8990/udp                # webmail HTTP service
https-wmap      8991/tcp                # webmail HTTPS service
https-wmap      8991/udp                # webmail HTTPS service
[root@127 shell_demo]# 
[root@127 shell_demo]# sed -n '/^(icmp|igmp)/p' /etc/protocols
[root@127 shell_demo]# sed -rn '/^(icmp|igmp)/p' /etc/protocols
icmp    1       ICMP            # internet control message protocol
igmp    2       IGMP            # internet group management protocol
[root@127 shell_demo]# sed -n '\cUIDcp' /etc/login.defs
UID_MIN                  1000
UID_MAX                 60000
SYS_UID_MIN               201
SYS_UID_MAX               999
[root@127 shell_demo]# sed -n '\xbashxp' /etc/shells
/bin/bash
/usr/bin/bash
[root@127 shell_demo]# sed -n '\1bash1p' /etc/shells
/bin/bash
/usr/bin/bash
[root@127 shell_demo]# sed -n '\:bash:p' /etc/shells
/bin/bash
/usr/bin/bash
[root@127 shell_demo]# sed -n '\,bash,p' /etc/shells
/bin/bash
/usr/bin/bash
[root@127 shell_demo]# sed -n -l' /etc/shells
> '
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
[root@127 shell_demo]# sed -n 'l' /etc/shells
/bin/sh$
/bin/bash$
/usr/bin/sh$
/usr/bin/bash$
[root@127 shell_demo]# sed -n '/root/=' /etc/passwd
1
10
[root@127 shell_demo]# sed -n '3=' /etc/passwd
3
[root@127 shell_demo]# sed -n '$=' /etc/passwd
28
[root@127 shell_demo]# sed -n 'l!p' /etc/hosts
sed: -e expression #1, char 2: extra characters after command
[root@127 shell_demo]# sed -n '1!p' /etc/hosts
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -n '2!p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -n '/bash/!p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -n '/bash/!p' /etc/shells
/bin/sh
/usr/bin/sh
[root@127 shell_demo]# sed -n '/sh/!p' /etc/shells
[root@127 shell_demo]# cp /etc/hosts /tmp/hosts
[root@127 shell_demo]# sed '1a add test line' /tmp/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
add test line
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -i '1a add test line' /tmp/hosts 
[root@127 shell_demo]# cat /tmp/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
add test line
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -i.bak '2d' /tmp/hosts
[root@127 shell_demo]# cat /tmp/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts.bak 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
add test line
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '1i add new line' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed -i '1i add new line' /tmp/hosts
[root@127 shell_demo]# cat /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '/new/a temp line' /tmp/hosts
add new line
temp line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '/new/a temp line;3p;2i addline' /tmp/hosts
add new line
temp line;3p;2i addline
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/
cat: /tmp/: Is a directory
[root@127 shell_demo]# cat /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed 'd' /tmp/hosts
[root@127 shell_demo]# sed '1d' /tmp/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cp /etc/profile /tmp/
[root@127 shell_demo]# sed -i '/^$/d' /tmp/profile 
[root@127 shell_demo]# sed -i '/^#/d' /tmp/profile 
[root@127 shell_demo]# sed '/local/d' /tmp/hosts
add new line
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '2c modify line' /tmp/hosts
add new line
modify line
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed 'c all modify line' /tmp/hosts
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
all modify line
[root@127 shell_demo]# sed '/new/c line' /tmp/hosts
line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /etc/hostname 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
[root@127 shell_demo]# sed 'r /etc/hostname' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 adhoc-1
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 adhoc-2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 namenode
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 datanode1
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 hiveserver
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 hivemetastore
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 kafkabroker
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 sparkmaster
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 zookeeper
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
[root@127 shell_demo]# sed '1r /etc/hostname' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '3r /etc/hostname' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.111   localhost
192.168.1.111   sky
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed 'w /tmp/myhosts' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/myhosts 
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# sed '1,3w /tmp/myhosts' /tmp/hosts
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 adhoc-1
127.0.0.1 adhoc-2
127.0.0.1 namenode
127.0.0.1 datanode1
127.0.0.1 hiveserver
127.0.0.1 hivemetastore
127.0.0.1 kafkabroker
127.0.0.1 sparkmaster
127.0.0.1 zookeeper
[root@127 shell_demo]# cat /tmp/myhosts 
add new line
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@127 shell_demo]# sed '3q' /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
[root@127 shell_demo]# vim test.txt
[root@127 shell_demo]# sed 's/hello/hi/' test.txt 
hi the world.
go spurs go.
123 456 789.
hi the beijing.
I am Jacob.
[root@127 shell_demo]# sed '1s/hello/hi/' test.txt 
hi the world.
go spurs go.
123 456 789.
hello the beijing.
I am Jacob.
[root@127 shell_demo]# cat test.txt 
hello the world.
go spurs go.
123 456 789.
hello the beijing.
I am Jacob.
[root@127 shell_demo]# sed 's/o/O' test.txt 
sed: -e expression #1, char 5: unterminated `s' command
[root@127 shell_demo]# 
[root@127 shell_demo]# sed 's/o/O' test.txt 
sed: -e expression #1, char 5: unterminated `s' command
[root@127 shell_demo]# sed 's/o/O/g' test.txt
hellO the wOrld.
gO spurs gO.
123 456 789.
hellO the beijing.
I am JacOb.
[root@127 shell_demo]# sed 's/o/O' test.txt
sed: -e expression #1, char 5: unterminated `s' command
[root@127 shell_demo]# sed 's/o/O/g' test.txt
hellO the wOrld.
gO spurs gO.
123 456 789.
hellO the beijing.
I am JacOb.
[root@127 shell_demo]# sed 's/o/O/2' test.txt
hello the wOrld.
go spurs gO.
123 456 789.
hello the beijing.
I am Jacob.
[root@127 shell_demo]# sed 's/e/E/2p' test.txt
hello thE world.
hello thE world.
go spurs go.
123 456 789.
hello thE beijing.
hello thE beijing.
I am Jacob.
[root@127 shell_demo]# sed 's/e/E/3p' test.txt
hello the world.
go spurs go.
123 456 789.
hello the bEijing.
hello the bEijing.
I am Jacob.
[root@127 shell_demo]# sed 's/e/E/gp' test.txt
hEllo thE world.
hEllo thE world.
go spurs go.
123 456 789.
hEllo thE bEijing.
hEllo thE bEijing.
I am Jacob.
[root@127 shell_demo]# sed 's/jacob/vicky/i' test.txt
hello the world.
go spurs go.
123 456 789.
hello the beijing.
I am vicky.
[root@127 shell_demo]# echo "/etc/hosts" | sed 's/^/ls -l /e'
-rw-r--r-- 2 root root 342 Apr  6  2022 /etc/hosts
[root@127 shell_demo]# echo "tmpfile" | sed 's#^#touch /tmp/#e'

[root@127 shell_demo]# ls -l /tmp/tmpfile 
-rw-r--r-- 1 root root 0 Jan  1 16:42 /tmp/tmpfile
[root@127 shell_demo]# sed 's/the//' test.txt 
hello  world.
go spurs go.
123 456 789.
hello  beijing.
I am Jacob.
[root@127 shell_demo]# echo '"hello" "world"'
"hello" "world"
[root@127 shell_demo]# echo '"hello" "world"'| sed 's/\".*\"//'

[root@127 shell_demo]# echo '"hello" "world"'| sed 's/\"[^\"\*\"//'
sed: -e expression #1, char 14: unterminated `s' command
[root@127 shell_demo]# echo '"hello" "world"'| sed 's/\"[^\"]*\"//'
 "world"
[root@127 shell_demo]# sed -r 's/^(.)(.*)(.)$/\3\2\1/' test.txt 
.ello the worldh
.o spurs gog
.23 456 7891
.ello the beijingh
. am JacobI
[root@127 shell_demo]# cp /etc/passwd /tmp/passwd 
cp: overwrite ‘/tmp/passwd’? y
[root@127 shell_demo]# cat /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 shell_demo]# sed 's/\/sbin\/nologin/\/bin\/sh/' /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/sh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/bin/sh
lp:x:4:7:lp:/var/spool/lpd:/bin/sh
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/sh
operator:x:11:0:operator:/root:/bin/sh
games:x:12:100:games:/usr/games:/bin/sh
ftp:x:14:50:FTP User:/var/ftp:/bin/sh
nobody:x:99:99:Nobody:/:/bin/sh
systemd-network:x:192:192:systemd Network Management:/:/bin/sh
dbus:x:81:81:System message bus:/:/bin/sh
polkitd:x:999:998:User for polkitd:/:/bin/sh
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/sh
postfix:x:89:89::/var/spool/postfix:/bin/sh
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/sh
redis:x:998:994:Redis Database Server:/var/lib/redis:/bin/sh
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/bin/sh
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/bin/sh
ntp:x:38:38::/etc/ntp:/bin/sh
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/bin/sh
[root@127 shell_demo]# sed 's#/sbin/nologin#/bin/sh#' /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/sh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/bin/sh
lp:x:4:7:lp:/var/spool/lpd:/bin/sh
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/sh
operator:x:11:0:operator:/root:/bin/sh
games:x:12:100:games:/usr/games:/bin/sh
ftp:x:14:50:FTP User:/var/ftp:/bin/sh
nobody:x:99:99:Nobody:/:/bin/sh
systemd-network:x:192:192:systemd Network Management:/:/bin/sh
dbus:x:81:81:System message bus:/:/bin/sh
polkitd:x:999:998:User for polkitd:/:/bin/sh
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/sh
postfix:x:89:89::/var/spool/postfix:/bin/sh
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/sh
redis:x:998:994:Redis Database Server:/var/lib/redis:/bin/sh
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/bin/sh
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/bin/sh
ntp:x:38:38::/etc/ntp:/bin/sh
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/bin/sh
[root@127 shell_demo]# sed 's,/sbin/nologin,/bin/sh,' /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/sh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/bin/sh
lp:x:4:7:lp:/var/spool/lpd:/bin/sh
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/sh
operator:x:11:0:operator:/root:/bin/sh
games:x:12:100:games:/usr/games:/bin/sh
ftp:x:14:50:FTP User:/var/ftp:/bin/sh
nobody:x:99:99:Nobody:/:/bin/sh
systemd-network:x:192:192:systemd Network Management:/:/bin/sh
dbus:x:81:81:System message bus:/:/bin/sh
polkitd:x:999:998:User for polkitd:/:/bin/sh
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/sh
postfix:x:89:89::/var/spool/postfix:/bin/sh
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/sh
redis:x:998:994:Redis Database Server:/var/lib/redis:/bin/sh
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/bin/sh
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/bin/sh
ntp:x:38:38::/etc/ntp:/bin/sh
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/bin/sh
[root@127 shell_demo]# sed 'sx/sbin/nologinx/bin/shx' /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/sh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/bin/sh
lp:x:4:7:lp:/var/spool/lpd:/bin/sh
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/sh
operator:x:11:0:operator:/root:/bin/sh
games:x:12:100:games:/usr/games:/bin/sh
ftp:x:14:50:FTP User:/var/ftp:/bin/sh
nobody:x:99:99:Nobody:/:/bin/sh
systemd-network:x:192:192:systemd Network Management:/:/bin/sh
dbus:x:81:81:System message bus:/:/bin/sh
polkitd:x:999:998:User for polkitd:/:/bin/sh
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/sh
postfix:x:89:89::/var/spool/postfix:/bin/sh
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/sh
redis:x:998:994:Redis Database Server:/var/lib/redis:/bin/sh
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/bin/sh
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/bin/sh
ntp:x:38:38::/etc/ntp:/bin/sh
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/bin/sh
[root@127 shell_demo]# sed 's9/sbin/nologin9/bin/sh9' /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/sh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/bin/sh
lp:x:4:7:lp:/var/spool/lpd:/bin/sh
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/sh
operator:x:11:0:operator:/root:/bin/sh
games:x:12:100:games:/usr/games:/bin/sh
ftp:x:14:50:FTP User:/var/ftp:/bin/sh
nobody:x:99:99:Nobody:/:/bin/sh
systemd-network:x:192:192:systemd Network Management:/:/bin/sh
dbus:x:81:81:System message bus:/:/bin/sh
polkitd:x:999:998:User for polkitd:/:/bin/sh
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/sh
postfix:x:89:89::/var/spool/postfix:/bin/sh
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/sh
redis:x:998:994:Redis Database Server:/var/lib/redis:/bin/sh
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/bin/sh
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/bin/sh
ntp:x:38:38::/etc/ntp:/bin/sh
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/bin/sh
[root@127 shell_demo]# cat test.txt 
hello the world.
go spurs go.
123 456 789.
hello the beijing.
I am Jacob.
[root@127 shell_demo]# 
[root@127 shell_demo]# sed -n '1p;3p;5p' test.txt 
hello the world.
123 456 789.
I am Jacob.
[root@127 shell_demo]# sed -n -e '1p' -e '3p' test.txt 
hello the world.
123 456 789.
[root@127 shell_demo]# sed '/world/s/hello/hi/;s/the//' test.txt 
hi  world.
go spurs go.
123 456 789.
hello  beijing.
I am Jacob.
[root@127 shell_demo]# sed '/world/{s/hello/hi/;s/the//}' test.txt 
hi  world.
go spurs go.
123 456 789.
hello the beijing.
I am Jacob.
[root@127 shell_demo]# vim script.sed
[root@127 shell_demo]# sed -f script.sed test.txt 
hello world
go spurs go.
Hello the china.
I am Jacob.
[root@127 shell_demo]# 
[root@127 shell_demo]# 
[root@127 shell_demo]# vim test.txt
[root@127 shell_demo]# sed '2h;5g' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
2:go spurs go.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '2h;5G' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
2:go spurs go.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '2H;5G' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.

2:go spurs go.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '2H;5g' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.

2:go spurs go.
6:Test hold/pattern space.
[root@127 shell_demo]# cat test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# 
[root@127 shell_demo]# sed '2h;2d;5g' test.txt 
1:hello the world.
3:123 456 789.
4:hello the beijing.
2:go spurs go.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '1h;4x' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
1:hello the world.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# cat test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '1h;2H;4x' test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
1:hello the world.
2:go spurs go.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# cat test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# sed 'n;d' test.txt 
1:hello the world.
3:123 456 789.
5:I am Jacob.
[root@127 shell_demo]# cat test.txt 
1:hello the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# sed -n '2{N;l}' test.txt 
2:go spurs go.\n3:123 456 789.$
[root@127 shell_demo]# sed -n '2{N;p}' test.txt 
2:go spurs go.
3:123 456 789.
[root@127 shell_demo]# sed -n '2{N;p}' test.txt 
2:go spurs go.
3:123 456 789.
[root@127 shell_demo]# sed -n 'N;s/\n//' test.txt 
[root@127 shell_demo]# sed  'N;s/\n//' test.txt 
1:hello the world.2:go spurs go.
3:123 456 789.4:hello the beijing.
5:I am Jacob.6:Test hold/pattern space.
[root@127 shell_demo]# sed 'y/hg/HG/' test.txt 
1:Hello tHe world.
2:Go spurs Go.
3:123 456 789.
4:Hello tHe beijinG.
5:I am Jacob.
6:Test Hold/pattern space.
[root@127 shell_demo]# sed 'y/hg/12/' test.txt 
1:1ello t1e world.
2:2o spurs 2o.
3:123 456 789.
4:1ello t1e beijin2.
5:I am Jacob.
6:Test 1old/pattern space.
[root@127 shell_demo]# sed 'y/hg/21/' test.txt 
1:2ello t2e world.
2:1o spurs 1o.
3:123 456 789.
4:2ello t2e beijin1.
5:I am Jacob.
6:Test 2old/pattern space.
[root@127 shell_demo]# sed 'y/abcde/ABCDE/' test.txt 
1:hEllo thE worlD.
2:go spurs go.
3:123 456 789.
4:hEllo thE BEijing.
5:I Am JACoB.
6:TEst holD/pAttErn spACE.
[root@127 shell_demo]# sed -n '=;p' test.txt 
1
1:hello the world.
2
2:go spurs go.
3
3:123 456 789.
4
4:hello the beijing.
5
5:I am Jacob.
6
6:Test hold/pattern space.
[root@127 shell_demo]# sed -n '/go/b label;=;:label;p' test.txt 
1
1:hello the world.
2:go spurs go.
3
3:123 456 789.
4
4:hello the beijing.
5
5:I am Jacob.
6
6:Test hold/pattern space.
[root@127 shell_demo]# sed 's/\./!/' test.txt 
1:hello the world!
2:go spurs go!
3:123 456 789!
4:hello the beijing!
5:I am Jacob!
6:Test hold/pattern space!
[root@127 shell_demo]# sed '/beijing/b end;s/\./!/;:end' test.txt 
1:hello the world!
2:go spurs go!
3:123 456 789!
4:hello the beijing.
5:I am Jacob!
6:Test hold/pattern space!
[root@127 shell_demo]# sed 's/hello/nihao/' test.txt 
1:nihao the world.
2:go spurs go.
3:123 456 789.
4:nihao the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# sed '/hello/{s/hello/nihao/;:next;n;b next}' test.txt
1:nihao the world.
2:go spurs go.
3:123 456 789.
4:hello the beijing.
5:I am Jacob.
6:Test hold/pattern space.
[root@127 shell_demo]# vim contact.txt
[root@127 shell_demo]# sed -r 'N;s/\n//;s/: +/: /' contact.txt 
phone:13312345678
mail:test@test.com
phone:13612345678
mail:beta@teta.com
[root@127 shell_demo]# sed -r ':start;/:$/N;s/\n +//;t start' contact.txt
phone:
13312345678
mail:
test@test.com
phone:
13612345678
mail:
beta@teta.com
[root@127 shell_demo]# vim contact.txt 
[root@127 shell_demo]# sed -r ':start;/:$/N;s/\n +//;t start' contact.txt
phone:
13312345678
mail:test@test.com
phone:13612345678
mail:
beta@teta.com
[root@127 shell_demo]# echo fe:54:00:8f:25:92 | sed 's/://g'
fe54008f2592
[root@127 shell_demo]# echo fe54008f2592 | sed -r 's/([^:]*)([0-9a-f]{2})/\1:\2/'
fe54008f25:92
[root@127 shell_demo]# echo fe54008f2592 | sed -r ':loop;s/([^:]+)([0-9a-f]{2})/\1:\2/;t loop'
fe:54:00:8f:25:92
[root@127 shell_demo]# cat /etc/ssh/ssh
ssh_config                ssh_host_ecdsa_key        ssh_host_ed25519_key      ssh_host_rsa_key
sshd_config               ssh_host_ecdsa_key.pub    ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
[root@127 shell_demo]# cat /etc/ssh/ssh
ssh_config                ssh_host_ecdsa_key        ssh_host_ed25519_key      ssh_host_rsa_key
sshd_config               ssh_host_ecdsa_key.pub    ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
[root@127 shell_demo]# cat /etc/ssh/sshd_config 
#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
[root@127 shell_demo]# cat /etc/default/grub 
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
[root@127 shell_demo]# cat /boot/grub2/
device.map  fonts/      grub.cfg    grubenv     i386-pc/    locale/     
[root@127 shell_demo]# /etc/grub.d/0
00_header  00_tuned   01_users   
[root@127 shell_demo]# /etc/grub.d/01_users 
if [ -f ${prefix}/user.cfg ]; then
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root ${GRUB2_PASSWORD}
  fi
fi

相关文章

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • shell基础

    shell入门

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • bash入门

    Bourne-Again SHell; Born Again SHell Introduction 写作“入门”,...

  • 2017读书清单

    技术 Linux类 Linux系统命令及Shell脚本实践指南ps:适合快速入门的一本书,看完只能对linux有个...

  • shell入门学习(1)——语法基础

    本文为转载,原文:shell入门学习(1)——语法基础 介绍 Shell Script,Shell脚本与Windo...

  • 0x01 Scala入门-基本shell交互操作

    Scala入门-基本shell交互操作

  • 2019-09-19

    Shell 概述 Shell 解析器 查看系统shell解析器 默认解析器为bash Shell 脚本入门 新建h...

  • shell脚本

    shell入门 脚本格式入门 脚本以!/bin/bash开头,指定解析器 第一个shell脚本 需求 创建shel...

  • 自动化脚本实践(Shell + Expect)

    Linux Shell脚本入门: Linux awk 命令 | 菜鸟教程 Shell 教程 | 菜鸟教程 lin...

网友评论

      本文标题:shell入门23-sed实践

      本文链接:https://www.haomeiwen.com/subject/zrwccdtx.html