HA

工作笔记2016-2021

停止网页重定向

window.addEventListener("beforeunload", () => { debugger; }, false);

JS转换字符为UNICODE

escape("转换字符").replace(/%/g, "\\").toLowerCase();
// 十进制unicode字符码转换为字符
String.fromCharCode(112)

JS获取一组元素取出属性并组合

// 取得链接url,空格连接
[...document.querySelectorAll('[id=thumbnail]')].map(item => item.href).join(" ")
Array.prototype.slice.call(document.querySelectorAll('[id=thumbnail]')).map(item => item.href).join(" ")
// 取得链接url,换行连接
[...document.querySelectorAll('[id=thumbnail]')].map(item => item.href).reduce((a, b) => {return a + "\n" + b;})

db2 表异常解决办法

connect to dbut user tom using password01
load query table ut.userinfo
reorg table ut.userinfo

db2 导出导入表数据

export to /path/userinfo_data.ixf of ixf select * from ut.userinfo fetch first 50 rows only

import from /path/userinfo_data.ixf of ixf insert into ut.userinfonew

java取得文件行数

Files.lines(Paths.get("/path/file")).count();

根据wsdl文件生成代码

wsimport -s /path/result/ file:/path/userinfo.wsdl -wsdllocation file:/serverpath/userinfo.wsdl -encoding utf-8 -Xnocompile

linux下查看特定文件的实时内容

tail -500f /path/result/info.log

IHS启动与停止,配置规则删除特定cookie

/usr/IBM/HTTPServer/bin/apachectl -k start -f /usr/IBM/HTTPServer/conf/httpd.conf
/usr/IBM/HTTPServer/bin/apachectl -k stop -f /usr/IBM/HTTPServer/conf/httpd.conf

# /usr/IBM/HTTPServer/conf/httpd.conf
Header edit Set-Cookie "^(JESSIONID=).*" "$1;Path=/;Expires=Thu, 01 Jan 1970 00:00:00 GMT"

查看linux发行版信息

cat /proc/version
# Linux version 5.4.0-132-generic (buildd@lcy02-amd64-059) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #148-Ubuntu SMP Mon Oct 17 16:02:06 UTC 2022
uname -a
# Linux vu18-VirtualBox 5.4.0-132-generic #148-Ubuntu SMP Mon Oct 17 16:02:06 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
# No LSB modules are available.
# Distributor ID:	Ubuntu
# Description:	Ubuntu 20.04.5 LTS
# Release:	20.04
# Codename:	focal

查看linux发行版信息

cat /proc/version
# Linux version 5.4.0-132-generic (buildd@lcy02-amd64-059) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #148-Ubuntu SMP Mon Oct 17 16:02:06 UTC 2022
uname -a
# Linux vu18-VirtualBox 5.4.0-132-generic #148-Ubuntu SMP Mon Oct 17 16:02:06 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
# No LSB modules are available.
# Distributor ID:	Ubuntu
# Description:	Ubuntu 20.04.5 LTS
# Release:	20.04
# Codename:	focal

计算文件hash值

openssl sha1 /path/file

vim操作

# 显示行数
:set nu
# 搜索
/keyword
# ENTER
n #to next occurrence 
N #to prev occurrence 
# 替换
:s/article/tutorial/g # 在当前行内全部替换
:%s/article/tutorial/g # 在当前文件内全部替换
:%s/article/tutorial/gi # 在当前文件内忽略大小写全部替换
# 删除字符
x
# 剪切行,也可用作删除
dd
# 复制行
yy 或者 Y
# 在当前行前粘贴
P
# 在当前行后粘贴
p

# 注释多行代码
Ctrl + V
使用箭头选择多行
Shift + i
输入# 
ESC + ESC

# 去掉注释
Ctrl + V
使用箭头选择多行
x

find命令查找文件

# 列出所有文件
find . -type f
# 查找指定名字的文件,文件夹
find . -name test.md
find . -name *.md
find . -regex '.*.md'
# 列出所有文件夹,层数1即当前目录
find . -maxdepth 1 -type d
# 列出所有层数大于10的文件夹
find . -mindepth 10 -type d
# 查找文件并替换对应内容(所有文件的修改日期都会变掉)
find . -type f -exec perl -pi -e 's/public/private/g' {} \;
find . -type f -exec sed -i 's/public/private/g' {} \;
sed -i 's/# requirepass foobared/requirepass xxx/' /etc/redis/redis_6487.conf
sed -i 's/# maxmemory <bytes>/maxmemory 52548800/' /etc/redis/redis_6487.conf
# 查找文件并替换对应内容(如果没有包含关键字则不修改文件,修改日期不会变)
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
# 查找文件并替换对应内容(包含/)
find . -type f -exec perl -pi -e 's#public#private#g' {} \;
find . -type f -exec sed -i 's#public#private#g' {} \;
# 查找最近被修改过的三个文件
find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'
# 查找最近时间段内被修改过的文件
# 180天
find . -type f -mtime -180
find . -type f -mtime -180 -exec ls -lrt {} \;
# 120分钟
find . -type f -mmin -120 -exec ls -lrt {} \;
# 复制目录结构
find . -type d > dirs.txt
xargs mkdir -p < dirs.txt
# 查找大于100m的文件
find . -type f -size +100M

每隔一段时间执行一段命令

# 每隔五秒钟打印一次磁盘空间信息
while true; do date; df -h; sleep 5; done
while true; do date; df -h | grep '/dev/sda1'; sleep 5; done
# -d 高亮变化的部分 -d 间隔(单位秒)
watch -d -n 5 df -h

使用scp复制远程服务器到本地,复制本地文件到远程服务器

scp -r -P 22 tom@ip:/path/file ./
scp -P 22 /local/path/file tom@ip:~/temp

复制文件夹,保持原来的权限和链接

cp -rpH /path/ ./

curl使用

# -k 忽略证书错误,-c 显示cookie
curl -k -c - https://www.baeldung.com/linux/vim-search-replace
# 在未安装curl的机器上请求url
perl -MLWP::Simple -e "getprint 'https://www.baeldung.com/linux/vim-search-replace'"
# 依次请求多个url
curl https://jsonplaceholder.typicode.com/todos/1 https://jsonplaceholder.typicode.com/todos/12
# 请求时使用用户名
curl -u username http://example.com

检查机器支持的ssl版本

openssl ciphers -v | awk '{print $2}' | sort | uniq
# 检查网站的ssl版本
openssl s_client -connect bing.com:443

正则表达式

# 大小写转换 
# \U$1 \L$1 全部转换为大写,小写
# \u$1 \l$1 首字母转换为大写,小写
# \p{Lu} \p{Ll} 匹配大写,小写字母
# USER_PET_NAME -> userPetName
([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+) -> \L$1\u$2\u$3
# USER_PET_NAME <- userPetName
(\p{Ll}+)(\p{Lu}\p{Ll}+)(\p{Lu}\p{Ll}+) -> \U$1_$2_$3
# 常用表达式
\s 空白字符[ \f\n\r\t\v],会匹配全角空格符
\S 非空白字符[^ \f\n\r\t\v]
\d 数字字符[0-9],会匹配全角数字字符
\D 非数字字符[^0-9]
\w 包括下划线的任何单词字符[A-Za-z0-9_],会匹配中文字符
\W 非单词字符[^A-Za-z0-9_]
# 正向肯定预查
(?=pattern)
(?:pattern)
# 正向否定预查
(?!pattern)
# 反向肯定预查
(?<=pattern)
# 反向否定预查
(?<!pattern)

查看linux命令位置和内容

whereis vim
type -a vim

文件中搜索字符串

# -i 忽略大小写
# -o 不输出整行,只输出符合搜索样式的部分
# -n 显示行数
# -A 显示检索结果行之后的特定行
# -B 显示检索结果行之前的特定行
# -e 指定检索字符串,可设置复数个
# -E 使用正则表达式
# -R 检索子目录
# -h 不显示文件名
grep -ion 'keyword' /path/file -A 50 -B 50
# 使用正则表达式搜索
egrep -i 'public|private' /path/file
# 在文件夹内特定后缀的文件中检索
grep -r 'cluster' --include \*.yml /path/
# 在压缩文件中检索(过去的log有时会被备份为gz)
zgrep -e 'cluster' -e 'another pattern' /path/*

查看linux进程

# 根据名字查找
ps -ef | grep java
# 根据监听端口号
lsof -i :8080
# 杀掉监听指定端口好的程序
kill -9 $(lsof -t -i:8080)
# 查找正在使用某一个文件的进程
fuser /path/file
ps -ef | grep PID

解压缩

# Create archive.tar from files foo and bar.
tar -cf archive.tar foo bar --exclude='my-base-dir/target' --exclude='my-base-dir/.git'
# List all files in archive.tar verbosely.
tar -tvf archive.tar
# Extract all files from archive.tar.
tar -xf archive.tar
# 提取tar包中的特定文件
tar -zxvf <tar filename> <file you want to extract>

Excel函数使用

# google表格也支持下面的函数
# 搜索 L2 单元格内的内容在 I4:I15 区域内出现的次数
=SUMPRODUCT(len(I4:I15)-len(SUBSTITUTE(I4:I15,L2,"")))/len(L2)
# 获取A2单元格内字符2右边的内容
=RIGHT(A4,len(A4)-find("2",A4))
# 获取A6单元格内字符2左边的内容
=LEFT(A6,find("2",A6)-1)
# 获取文件后缀名
=UPPER(RIGHT(C10,len(C10)-find(".",C10)))
# 获取所在列(E)所在单元格上面非空的单元格数量(也可从特定行(E1)开始计算)
=counta(E1:INDIRECT("E" & ROW() - 1)) + 1
# 获取所在列以外特定列(I)中最后一个非空值
=ARRAY_CONSTRAIN(ARRAYFORMULA(LOOKUP(2,1/(I:I<>""),I:I)), 1, 1)
# 获取特定列(I)中(也适用于所在列)最后一个非空值
=ARRAY_CONSTRAIN(ARRAYFORMULA(LOOKUP(2,1/(INDIRECT("I1"):INDIRECT("I" & ROW() - 1)<>""),INDIRECT("I1"):INDIRECT("I" & ROW() - 1))), 1, 1)
# 获取特定单元格(A2)的值,如果为空,取另一个单元格(B1)的值
=IF(ISBLANK(A2),B1,A2)
# 取得两个单元格的值相加
=A1&B2
# 比较四列数据, 如果A1中的值在C列中存在(CX),且B1=DX则为1, 在C列存在但B1!=DX否则为0, 在C列不存在则为#N/A
=IF(AND(ISNUMBER(MATCH(A1,C:C,0)),B1=VLOOKUP(A1,C:D,2,0)),1,0)

reference