grep、sed、awk命令练习

1.显示所有包含San的行
grep ‘San’ 1data.txt
sed -n ‘/San/p’ 1data.txt
awk -F ‘:’ ‘/San/ {print $0}’ 1data.txt ##显示整行,不需要分隔符
awk ‘/San/ {print $0}’ 1data.txt

2.显示所有以J开始的人名所在的行
grep -E ‘^J’ 1data.txt
sed -n ‘/^J/p’ 1data.txt
awk '/^J/ {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 0}̲' 1data.txt 3.…’ 1data.txt
sed -n ‘/700 / p ′ 1 d a t a . t x t a w k ′ / 700 /p' 1data.txt awk '/700 /p1data.txtawk/700/ {print $0}’ 1data.txt
4.显示所有不包括23850的行
grep -v ‘23850’ 1data.txt
sed -n ‘/[^23850]/p’ 1data.txt
awk ‘/[^23850]/ {print $0}’ 1data.txt ##错误,awk的不包含?
5.显示所有生日在December的行
grep -E ‘11/\d{1,2}/\d{1,2}’ ##\d这里不支持
grep -E ‘11/[0-9]{1,2}/[0-9]{1,2}’
sed -n ‘/11/[0-9]{1,2}/[0-9]{1,2}/p’ ##长度用{},但需要转义
awk -F ‘:’ ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 7: (NF-1)}̲' 1data.txt| gr…(NF-1/==^11 {print )}’ 1data.txt
6.显示所有电话号码的区号为674的行
grep -E ‘674-\d{3}-\d{4}’ ##不符合,不支持/d
grep -E ‘674-[0-9]{3}-[0-9]{4}’ 1data.txt
sed -n ‘/674-\d{3}-\d{4}/p’ 1data.txt
sed -n ‘/674-[0-9]{3}-[0-9]{4}/p’ 1data.txt
awk -F ‘:’ ‘{print $2}’|grep ‘^674’ 1data.txt

7.显示所有这样的行:它包含一个大写字母,后跟四个小写字母,一个空格,和一个大写字母
grep -E ‘[A-Z]{1}{a-z}{4} [A-Z]{1}’ 1data.txt
sed -n ‘/[A-Z]{1}{a-z}{4} [A-Z]{1}/p’

8.显示姓以K或k开头的行
9.显示工资为六位数的行,并在前面加行号
grep -En ‘[0-9]{6} ′ 1 d a t a . t x t s e d − n ′ / [ 0 − 9 ] / 6 / ' 1data.txt sed -n '/[0-9]/{6/} 1data.txtsedn/[09]/6//p’ ##不符合
10.显示包括Lincoln或lincoln的行,并且grep对大小写不敏感.
grep -i ‘lincoln’ 1data.txt
sed -ni ‘/lincoln/p’ 1data.txt ##不符合

猜你喜欢

转载自blog.csdn.net/heroicpoem/article/details/106448976