sed or awk processes the implementation of the last line of a file _linux shell

Source: Internet
Author: User

Like the following text:
ABCD 1234 FDS
2011 550
1023 832er 1231

The request is processed as follows:
ABCD 1234 FDS
2011 550
total1:1023 Total2:832er 12end

Determine if the last line of text is preceded by the first column with Total1: The second column plus Total2: The third column is followed by minus two characters followed by the end.

Implemented using SED and awk, respectively.
Sed:

Copy Code code as follows:

Echo ' ABCD 1234 FDS
2011 550
1023 832er 1231 ' |sed-r ' $s/([^]*) () ([^]*) () (..). *$/total1:\1\2total:\3\4\5end/'

ABCD 1234 FDS
2011 550
total1:1023 Total:832er 12end

Awk:

Copy Code code as follows:

awk ' Nr>1{print a}{a=$0}end{$1= ' total1: "$1;$2=" Total2: "$2;print gensub (/. $/, "End", $)} ' file
awk ' Begin{getline a}{print a;a=$0}end{$1= total1: $1;$2= ' total2: ' $2;$3=gensub (/. $/, "End", 1,$3);p rint} '
awk ' {if (!getline line) {print ' Total1: "$" total2: "$" gensub (/. $/, "End", 1,$3)}else{print line?$0 RS line:$0}} '

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.