千家信息网

linux用什么命令处理文本?

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,1、grep2、正则表达式3、扩展正则表达式4、vim###########################cat -Acat -nscat -btacless : pgup; pgdown? n/N
千家信息网最后更新 2025年02月06日linux用什么命令处理文本?

1、grep

2、正则表达式

3、扩展正则表达式

4、vim

###########################

cat -A

cat -ns

cat -b

tac

less : pgup; pgdown

? n/N

/

openssl rand -base64 100 |tr -dc "[:alnum:]"|head -c12

[root@centos6 /app]#openssl rand -base64 100 |tr -dc "[:alnum:]"|head -c124oa9aPP1tr9I


tail -f 类似:tailf

tail -F 跟踪文件名

date -d "-1day"

[root@centos6 /app]#cut -d : -f 1,3 /etc/passwd

df |grep ^/dev/sd |sed -r "s/.*[ ]+([0-9]{1,3})%.*/\1/"df -i  |grep ^/dev/sd |sed -r "s/.*[ ]+([0-9]{1,3})%.*/\1/"[root@node4~]#ifconfig  eth0  |head -n2 |tail -n1 |tr -s " "  |cut -d " " -f3192.168.137.47[root@centos6 ~]#ifconfig  eth0  |head -n2 |tail -n1 |cut -d ":" -f2 |cut -d " " -f1192.168.137.6

[root@node4/app]#cut -d: -f1,3 /etc/passwd |sort -t: -k2 -n

root:0

bin:1

daemon:2

adm:3

lp:4


uniq -c

[root@node4/app]#cat /var/log/httpd/access_log |cut -d " " -f1 |sort |uniq -c |sort -nr

7 192.168.137.1

2 192.168.0.118


diff

patch

[root@node4/app]#cat f1 whereis[root@node4/app]#cat f2whosi[root@node4/app]#diff -u f1 f2 > diff.log[root@node4/app]#rm -f f1[root@node4/app]#patch -b f2  diff.log patching file f2Reversed (or previously applied) patch detected!  Assume -R? [n] y[root@node4/app]#mv f2 f1 [root@node4/app]#mv f2.orig  f2 [root@node4/app]#cat f1 f2whereiswhosi


linux 文本处理三剑客:

grep :

man grep


grep -nA3 root /etc/passwd

grep -c

[root@node4/app]#grep -nA3 root /etc/passwd


1:root:x:0:0:root:/root:/bin/bash2-bin:x:1:1:bin:/bin:/sbin/nologin3-daemon:x:2:2:daemon:/sbin:/sbin/nologin4-adm:x:3:4:adm:/var/adm:/sbin/nologin--10:operator:x:11:0:operator:/root:/sbin/nologin11-games:x:12:100:games:/usr/games:/sbin/nologin12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@node4/app]#nmap -v -sP 192.168.137.0/24  |grep  -B1 'up '  Nmap scan report for 192.168.137.1 (192.168.137.1)Host is up (0.00089s latency).--Nmap scan report for 192.168.137.6 (192.168.137.6)Host is up (0.00062s latency).[root@node4/app]#cat nmap.log   |grep report  |awk '{print $5}' 192.168.137.1192.168.137.6


grep -e 表示或的关系:

grep -e root -e bash

过滤单词:

[root@node4/app]#echo "x-abc-y" |grep abc

x-abc-y

man 7 regex


字符匹配:. 匹配任意单个字符[] 匹配指定范围内的任意单个字符[^] 匹配指定范围外的任意单个字符[:alnum:] 字母和数字[:alpha:] 代表任何英文大小写字符,亦即 A-Z, a-z[:lower:] 小写字母 [:upper:] 大写字母[:blank:] 空白字符(空格和制表符)[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)[:cntrl:] 不可打印的控制字符(退格、删除、警铃...)[:digit:] 十进制数字 [:xdigit:]十六进制数字[:graph:] 可打印的非空白字符[:print:] 可打印字符[:punct:] 标点符号
匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数* 匹配前面的字符任意次,包括0次贪婪模式:尽可能长的匹配.* 任意长度的任意字符\? 匹配其前面的字符0或1次 \+ 匹配其前面的字符至少1次 \{n\} 匹配前面的字符n次 \{m,n\} 匹配前面的字符至少m次,至多n次 \{,n\} 匹配前面的字符至多n次 \{n,\} 匹配前面的字符至少n次位置锚定:定位出现的位置^ 行首锚定,用于模式的最左侧$ 行尾锚定,用于模式的最右侧^PATTERN$ 用于模式匹配整行^$ 空行^[[:space:]]*$ 空白行\< 或 \b 词首锚定,用于单词模式的左侧\> 或 \b 词尾锚定;用于单词模式的右侧\ 匹配整个单词


[root@node4~]#df |grep /dev/sd |grep -o "[0-9]\+%" |grep -o "[0-9]\+" |sort -nr |head -n1

29


[root@node4~]#echo rootrbbt |grep "\([r..t]\).*\1"

rootrbbt

[root@node4~]#echo rootrbbt |grep "\(r..t\).*\1"

[root@node4~]#echo rootroot |grep "\(r..t\).*\1"

rootroot

[root@node4~]#cat /etc/passwd |grep "^\(.*\):.*/\1$"

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

[root@node4~]#cat /etc/passwd |egrep "^(.*):.*/\1$"

axy

[root@node4~]#echo axy |egrep "(a|b)xy"

axy

[root@node4~]#cat /etc/redhat-release|grep -o " [0-9]\+." |grep -o "[0-9]\+"

7

[root@node4~]#cat /etc/centos-release |grep -o "[0-9]\+" |head -n1

7

[root@node4~]#echo /etc/rc.d/init.d/ |egrep -o [^/]*/?$

init.d/

[root@node4~]#egrep -w "^(root|sun)" /etc/passwd|cut -d: -f1,7

root:/bin/bash

sun:/bin/bash


id地址:

[0-9]

[1-9][0-9]

1[0-9][0-9]

2[0-4][0-9]

25[0-5]



0