只输出进程的ID号,进程名字为adb。
[root@localhost ~]# ps -ef|grep mysql
mysql 3361 1 1 21:06 ? 00:00:01 /usr/sbin/mysqld
root 3460 3435 0 21:08 pts/0 00:00:00 grep --color=auto mysql
# 方法一
[root@localhost ~]# pgrep -f mysql # 仅输出进程ID号
3361
# 方法二
[root@localhost ~]# ps -ef|grep mysql|awk '{print $2}'
3361
3486
替换指定的目录及其子目录下所有文件中的adb为bcd。( stream editor 简写sed )
# 方法一
[root@localhost test]# find -type f | xargs cat
adc
adc
[root@localhost test]# find -type f |xargs sed -i 's/adc/bcd/g'
[root@localhost test]# find -type f | xargs cat
bcd
bcd
# 方法二
[root@localhost test]# sed -i 's/adc/bcd/g' `grep "adc" -rl ./`
# 注意:grep内容是用的反引号
替换某个文件中的内容adb为bcd(替换指定文件的字符串)。
[root@localhost test]# cat hello.txt
i love you
[root@localhost test]# sed -i "s/i/I/g" hello.txt
[root@localhost test]# cat hello.txt
I love you
1.给定一个字符串,例如abcabcd,请你求得到该字符串中所有的长度大于等于2的子串,并统计每个字串出现的次数。
# 字符串切片法
string = "abcabcd"
if len(string) < 2:
print("字符串不包含字串")
else:
subs = {}
for i in range(0, len(string)-1):
for j in range(i+2, len(string)):
sub_string = string[i:j]
if sub_string in subs:
subs[sub_string] += 1
else:
subs[sub_string] = 1
print(sorted(subs.items(), key=lambda items:items[-1], reverse=True))
# 输出结果
[('ab', 2), ('abc', 2), ('bc', 2), ('abca', 1), ('abcab', 1),
('abcabc', 1), ('bca', 1), ('bcab', 1), ('bcabc', 1), ('ca', 1),
('cab', 1), ('cabc', 1)]
2.输出字符串的所有连续子字符串长度要求大于等于2。
def cut(s: str):
results = []
num = 0
# x + 1 表示子字符串长度
for x in range(1, len(s)):
# i 表示偏移量
for i in range(len(s) - x):
results.append(s[i:i + x + 1])
return results
print(cut('1234'))
# 输出结果
['12', '23', '34', '123', '234', '1234']