Use __linux for awk and sub/gsub in field interception and substitution in Linux

Source: Internet
Author: User

The Gsub function causes the substitution to occur when all regular expressions are matched

Gsub (Regular expression, subsitution string, target string), short gsub (r,s,t)

Sub matches the first occurrence of a conforming pattern string, equivalent to sed ' s//'.
Gsub matches all matching pattern strings, equivalent to sed ' s//g '

Instance One
Remove a number from the second field
echo "0001|20081223efskjfdj| EREADFASDLKJCV "|awk-f ' |" ' Begin{ofs= ' |} {gsub (/[0-9]+/, "", 2);p rint 2);p rint 0} '
0001|efskjfdj| Ereadfasdlkjcv

Example Two
/split, take out the snapshots to know the field between the penultimate fourth (where/the number is not sure, so it is not convenient to directly use awk 6, 6,7,$8 to directly fetch, use the field interception),. Separate com.netfinworks.authorize
Example

/users/yfan/downloads/dsc20170801_jar/snapshots/com/netfinworks/authorize/authorize-service/1.0.0-snapshot/ Authorize-service-1.0.0-snapshot.jar

Set a variable first

Yfandemacbook-pro:pbs yfan$ test=/users/yfan/downloads/dsc20170801_jar/snapshots/com/netfinworks/authorize/ Authorize-service/1.0.0-snapshot/authorize-service-1.0.0-snapshot.jar

Use symbol truncation (# #) in Linux to remove the front part of COM

Yfandemacbook-pro:pbs yfan$ Echo ${test##*snapshots/}

Results

Com/netfinworks/authorize/authorize-service/1.0.0-snapshot/authorize-service-1.0.0-snapshot.jar

In awk, combine gsub to get rid of the back section (penultimate, second, third field)

Yfandemacbook-pro:pbs yfan$ echo ${test##*snapshots/}|awk-f '/' {gsub ("/" $ (NF-2) "/" $ (NF-1) "/" $NF, "");p rint} '

Results

Com/netfinworks/authorize

Finally, use SED. To replace/to accomplish a goal

Yfandemacbook-pro:pbs yfan$ echo ${test##*snapshots/}|awk-f '/' {gsub ("/" $ (NF-2) "/" $ (NF-1) "/" $NF, "");p rint} ' |sed  ' s/\//./g '

Results

Com.netfinworks.authorize

Gsub Description Reference:
Http://blog.sina.com.cn/s/blog_67e34ceb0100ybvg.html

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.