Use awk to process tagged text

Source: Internet
Author: User

When using awk to process tagged text, assume there is a string like "123 <em> abc </em> 456 <em> def </em> 789 <em> ghi </em> "the string in the question, to intercept the length of 5, the returned string should be 123ab. to intercept the length of 8, 123 <em> abc </em> 45 should be returned. Appendix: 1 <em> and </em> labels cannot be counted within the length. 2. The original <em> tag must be retained for the truncated string. However, if the last tag is not closed, the start tag is removed. Code: as follows #! /Bin/basha = '2014 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'echo "test text is' $ a' "read-p" please input a number little than 'echo $ a | awk' {gsub ("<em >|</em> ", "", $0); printf length ($0 )}'': "num echo $ a | awk-vnum =" $ num "-vFS = '</em>'' {for (I = 1; I <= NF; I ++) {orisum = sum; sum + = length ($ I)-4; if (sum <= num) {strlist = strlist ~ /./? Strlist "" FS "" $ I: $ I} else {sub ("<em>", "", $ 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 '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 212 [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 5123ab [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 7123 <em> abc </em> 4 [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 10123 <em> abc </em> 456d [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 9123 <em> abc </em> 456 [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 14123 <em> abc </em> 456 <em> def </em> 78 [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 17123 <em> abc </em> 456 <em> def </em> 789gh [root @ localhost ~] # Bash 1.sh test text is '2017 <em> abc </em> 123 <em> def </em> 456 <em> ghi </em> 'Please input a number little than 18: 18123 <em> abc </em> 456 <em> def </em> 789 <em> ghi </em> [root @ localhost ~] #

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.