如何使用 cut 和 grep 在 unix 中打印一系列字符?

How to print a range of character in unix using cut and grep?

我可以使用 (cut -d) 指定分隔符来打印字符块。

# grep -i access.log| grep ' 500 '| cut -d\- -f1 

但是如何在特定模式“address=tel%3A%2B”之后打印字符

例如:如何打印此文件中的整个 phone 个数字?

begining of the line address=tel%3A%2B416xxxxxxx rest of the log
begining of the line address=tel%3A%2B647xxxxxxx rest of the log
begining of the line address=tel%3A%2B519xxxxxxx rest of the log

...

sed -e 's,.*address=tel%3A%2B\([^ ]\+\).*,,g' <<EOF                          
begining of the line address=tel%3A%2B416xxxxxxx rest of the log                
begining of the line address=tel%3A%2B647xxxxxxx rest of the log                
begining of the line address=tel%3A%2B519xxxxxxx rest of the log                
EOF

输出:

416xxxxxxx
647xxxxxxx
519xxxxxxx

我尝试 cut -c 打印行中特定位置的字符,并且它起作用了,因为大多数行都遵循相同的模式。 直线的位置从1开始。

# grep -i test.log| grep ' 500 '|cut -c1-5 

这里1:是字符的起始位置

此处 5:要打印的最后一个字符的位置。

所以例如如果:

#cat test.log
this is address=tel%3A%2B416xxxxxxx
this is address=tel%3A%2B647xxxxxxx
this is address=tel%3A%2B519xxxxxxx

#cut -c26-36 test.log 
416xxxxxxx
647xxxxxxx
519xxxxxxx

这不是 grep and/or cut 的工作,它是 sed 或 awk 的工作:

$ sed 's/.*address=tel%3A%2B\([^ ]*\).*//' file
416xxxxxxx
647xxxxxxx
519xxxxxxx

$ awk '{gsub(/.*address=tel%3A%2B| .*/,"")}1' file
416xxxxxxx
647xxxxxxx
519xxxxxxx