1、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
方法一:[[email protected]7 fh]# echo $(tr ‘ ‘ ‘+‘ <file1) | bc 55 方法二:[[email protected]7 fh]# x=$(($(tr ‘ ‘ ‘+‘ <file1))) && echo $x 55 方法三:[[email protected]7 fh]# x=$(tr ‘ ‘ ‘+‘ <file1) | python -c "print($x)" 55
2、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[[email protected]7 fh]# echo xt.,l 1 jr#mn2 c*/fe3 uz4 | grep -oP ‘\d|\s‘ | gawk ‘BEGIN{ORS=""}{print}END{print"\n"}‘ 1 2 3 4
3、将PATH变量每个目录显示在独立的一行
方法一:[[email protected]7 fh]# echo $PATH | awk ‘BEGIN{RS=":"}{print}‘ /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin /home/fh/.local/bin /home/fh/bin 方法二: [[email protected]7 fh]# echo $PATH | tr ":" "\n" /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin /home/fh/.local/bin /home/fh/bin
4、删除指定文件的空行
sed -i ‘/^$/d‘ file
5、将文件中每个单词(字母)显示在独立的一行,并无空行
[[email protected]7 fh]# cat testfile2 chen +sljfl s [[email protected]7 fh]# gawk ‘BEGIN{RS=" "}{print}‘ testfile2 | sed ‘/^$/d‘ chen +sljfl s [[email protected]7 fh]# grep -oP "." testfile2 | sed ‘/^ *$/d‘ c h e n + s l j f l s
6、创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution"
[[email protected]7 fh]# useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo [[email protected]7 fh]# grep gentoo /etc/passwd gentoo:x:1002:1002:Gentoo Distribution:/home/gentoo:/bin/csh
7、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
方法一: [[email protected]7 fh]# tr ‘[a-z]‘ ‘[A-Z]‘ < /etc/issue > /tmp/issue.out && cat /tmp/issue.out \S KERNEL \R ON AN \M 方法二: [[email protected]7 fh]# sed ‘s/[a-z]/\u&/g‘ /etc/issue > /tmp/issue.out && cat /tmp/issue.out \S KERNEL \R ON AN \M
问题扩展:将/etc/issue文件中的每个单词的首(末)字母转换为大写(或小写)后保存至/tmp/issue.out文件中
首字母转大写: [[email protected]7 fh]# sed ‘s/\b[a-z]/\u&/g‘ /etc/issue > /tmp/issue.out && cat /tmp/issue.out \S Kernel \R On An \M 首字母转小写: [[email protected]7 fh]# sed ‘s/\b[A-Z]/\l&/g‘ /etc/issue > /tmp/issue.out && cat /tmp/issue.out \s kernel \r on an \m 末字母转小写: [[email protected]7 fh]# sed ‘s/[A-Z]\b/\l&/g‘ /etc/issue > /tmp/issue.out && cat /tmp/issue.out \s Kernel \r on an \m
8、将当前系统登录用户的信息转换为小写
[[email protected]7 ~]# w | tr ‘[A-Z]‘ ‘[a-z]‘ 06:15:58 up 2:39, 2 users, load average: 0.00, 0.01, 0.02 user tty from login@ idle jcpu pcpu what fh pts/0 172.18.21.244 05:16 3:10 0.15s 0.05s sshd: fh [priv] root pts/1 172.18.21.244 06:12 6.00s 0.00s 0.00s w [[email protected]7 ~]# w | sed ‘s/[A-Z]/\l&/g‘ 06:16:15 up 2:39, 2 users, load average: 0.00, 0.01, 0.02 user tty from login@ idle jcpu pcpu what fh pts/0 172.18.21.244 05:16 3:27 0.15s 0.05s sshd: fh [priv] root pts/1 172.18.21.244 06:12 7.00s 0.00s 0.00s w
9、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[[email protected]7 ~]# ls -a | gawk ‘BEGIN{ORS=" "}{print}‘ . .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc anaconda-ks.cfg tmpfile [[email protected]7 ~]# ls -a | tr ‘\n‘ ‘ ‘ . .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc anaconda-ks.cfg tmpfile
10、创建下面的用户、组和组成员关系
名字为admins 的组
[[email protected]7 fh]# groupadd admins [[email protected]7 fh]# grep admins /etc/group admins:x:1003:
用户natasha,使用admins 作为附属组
[[email protected]7 fh]# useradd -G admins natasha [[email protected]7 fh]# groups natasha natasha : natasha admins
用户harry,也使用admins 作为附属组
[[email protected]7 fh]# useradd -G admins harry [[email protected]7 fh]# groups harry natasha : harry admins
用户sarah,不可交互登录系统,且不是admins 的成员,natasha,harry,sarah密码都是centos
[[email protected]7 fh]# useradd -s /sbin/nologin sarah [[email protected]7 fh]# groups sarah sarah : sarah
[[email protected] fh]# echo ‘centos‘ | passwd --stdin natasha Changing password for user natasha. passwd: all authentication tokens updated successfully. [[email protected] fh]# echo ‘centos‘ | passwd --stdin harry Changing password for user harry. passwd: all authentication tokens updated successfully. [[email protected] fh]# echo ‘centos‘ | passwd --stdin sarah Changing password for user sarah. passwd: all authentication tokens updated successfully.
时间: 2024-10-16 14:15:03