Linux shell grep exercise

file content used

1: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:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pesign:x:995:992:Group for the pesign signing daemon:/var/run/pesign:/sbin/nologin
rngd:x:994:991:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
jiang:x:1000:1000:jiang:/home/jiang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
shell:x:1001:1001::/home/shell:/bin/bash
H_test:x:1002:1002::/home/H_test:/bin/bash

2: test.txt is used to replace all missing files

Carline, ah, how to begin to aloud again case old?
to you to

1. Display the lines starting with size-insensitive h in the /etc/passwd file;

1
egrep -i "^h" /etc/passwd
2
grep -ie '^h' /etc/passwd
3
grep -E '^h|^H' /etc/passwd

Insert image description here

2. Display the lines ending with sh in /etc/passwd;

grep -e 'sh$' /etc/passwd

Insert image description here

3. Display lines in /etc/fstab that begin with #, followed by one or more blank characters, and then followed by any non-blank characters;

grep -E '^#[[:space:]]+[^[:space:]]+' /etc/fstab

Insert image description here

4. Search for strings containing "starting with to and ending with to" in /etc/rc.d/rc.local;

grep -w '^to .* to$' test.txt

Insert image description here

5. Find lines in /etc/inittab that contain the pattern "words starting with s and ending with d";

grep '\<s[a-Z]*d\>' /etc/inittab

Insert image description here

6. Find the integer between 1-255 in the ifconfig command result;

ifconfig | grep -Eo  '\<[1-9]\>|\<[0-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-5][0-5]\>'

Insert image description here

7. Display the lines containing "Failed" or "FAILED" in the /var/log/secure file;

egrep -io 'failed' /var/log/secure 

Insert image description here

8. Take out the line in /etc/passwd where the default shell is bash;

Insert image description here

grep '/bin/bash' /etc/passwd

Insert image description here

9. List the file information starting with ns and ending with .conf in the /etc/ directory in long format;

grep '^\<sudo\>.*\.\<conf\>' test.txt

Insert image description here

10. Highlight the colon and the characters on both sides of it in the passwd file;

grep  --color=auto '[[:alnum:]]*:[[:alnum:]]*' passwd

Insert image description here
Um, this might be wrong

sed

11 Delete the blank characters at the beginning of all lines starting with blanks in the /etc/grub2.conf file

sed -e 's/^[[:space:]]*//g' grub2.cfg

Insert image description here

12. Delete the # and blank characters at the beginning of all lines in the /etc/fstab file that begin with # and are followed by at least one blank character.

sed -r 's/^#[[:space:]]+//g' grub2.cfg

-r Use extended regular expressions
![Insert image description here](https://img-blog.csdnimg.cn/31fff61559c9472781a69e63ad44d9aa.png)

13. Add # at the beginning of each line in /root/install.log

This machine does not have a /root/install.log file. The test.txt file is used instead.

sed -e 's/^.*$/#&/g' test.txt

Insert image description here

14. Add # to the beginning of lines that do not start with # in the /etc/fstab file.

sed 's/^[^#].*$/#&/' test.txt

Insert image description here

15. Use sed to get the IPv4 address of the machine in the ifconfig command.

ifconfig | sed -n '2p' | sed -r 's/^[^0-9]+([0-9.]+).*/\1/'

Insert image description here

16. Turn off the local SELinux function


17. Add content to the /etc/hosts configuration file


Guess you like

Origin blog.csdn.net/m0_51828898/article/details/128565113