linux下处理密码字典、文本文件

一般的密码字典都是文本文档,一行一个;
目标文件 dic.txt

使用linux下的命令来处理很方便:

仅去重

uniq -u dic.txt > dic-uniq.txt

注:这里的去重只能去除连续相同的重复

排序去重

sort dic.txt | uniq -u > dic-sort-uniq.txt

或者

sort -u dic.txt > dic-sort-u.txt

筛选大于8位字符串

awk '{if(length>7) print $0}' dic.txt > dic-length8+.txt

删除指定列

awk -F "----" '{if ($3) {print $2"----"$3}else{print $1"----"$2}}' test.txt > test2.txt

筛选符合正则的字符串

awk '/pattern/' dic.txt > dic-regexp.txt

多文件合并

# 将文件依次合并
cat file01 file02 file03 > all_file.txt

#将文件按列左右以英文逗号进行合并
paste -d ',' file01 file02 file03 > all_file.txt

# 合并同一类型文件
cat *.txt > dic

大文件分割

# 指定M切分
split -b 10m dic.txt -d -a 3 dic_

# 指定行切分
split -l 1000 dic.txt

文本处理扩展使用:

使用 sed 查找显示指定行内容

# 显示第8665125行内容
sed -n "8665125p" passwd.txt

使用 grep 查找包含指定内容的行

 grep -a "password--" password.txt

注:有时查找Windows下的文本文件会提示Binary file (standard input) matches 加上 -a 参数可强制转换为文本显示

使用 sed 删除指定行

#删除第1000行内容
sed -i '1000 d' filename.txt

使用 sed 全局替换字符

#将字符串 ---- 替换为 ,
sed -i 's/----/,/g' filename.txt

AWK求文件最长、最短行

# 最长行
awk '{if (length(max)<length()) max=$0}END{print max}’ filename.txt
# 最短行
awk '(NR==1||length(min)>length()){min=$0}END{print min}' filename.txt
Author: thinkwei

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注