Use awk to process tagged text. assume that & quot; 123 & lt; em & gt; abc & lt;/em & gt; 456 & lt; em & gt; def & lt; /em & gt; 789 & lt; em & gt; ghi & lt;/em & gt; & quot; the length of a string in such a string is 5, the returned string should be 123ab. to intercept 8 characters, 123 should be returned. & use awk to process tagged text. suppose there is "123 ".
Abc456
Def789
Ghi"To intercept the length of a string of 5, the returned string should be 123ab. to intercept the length of 8, 123 should be returned.
Abc45. Appendix: 1
AndThe tag cannot be counted within the length. 2. the truncated string must be retained.
Label, but if the last tag is not closed, remove its start tag. Code: as follows #! /Bin/basha = '100Abc456Def789Ghi'Echo "test text is '$ A'" read-p "please input a number little than 'echo $ a | awk' {gsub ("|"," ", $0); printf length ($0)}'': "num echo $ a | awk-vnum =" $ num "-vFS ='''{For (I = 1; I <= NF; I ++) {orisum = sum; sum + = length ($ I)-4; if (sum <= num) {strlist = strlist ~ /./? Strlist "" FS "" $ I: $ I} else {sub ("
"," ", $ I); strlist = strlist ~ /./? Strlist "" FS "" substr ($ I, 1, num-orisum): substr ($ I, 1, num-orisum); break} print strlist} 'test result: [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 212 [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 5123ab [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 7123Abc4 [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 10123Abc456d [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 9123Abc456 [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 14123Abc456Def78 [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 17123Abc456Def789gh [root @ localhost ~] # Bash 1.sh test text is '123Abc456Def789Ghi'Please input a number little than 18: 18123Abc456Def789Ghi[Root @ localhost ~] #
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.