Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

linux - awk string substr and cut not need char

I need to delete some char on string, example I have this string

$ tst=/oracle/orcl/dbms1911c
$ printf '%s' ${tst} | awk -F'/' '{print substr($4,5,6)}'
$1911c

How can I remove the 11 and my output will be 19c? I want only this when the string is not 19c.

Thanks in advance

question from:https://stackoverflow.com/questions/65924439/awk-string-substr-and-cut-not-need-char

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on your shown samples, could you please try following.

1st solution: Create a variable with substr of awk and then play with it to get exactly needed shown value, this approach will be useful in case we need to use this variable for further in the program too. Where tst=/oracle/orcl/dbms1911c is the variable's value as per shown samples.

printf '%s' ${tst} | 
awk -F'/' '{val=substr($4,5,6);print substr(val,1,2) substr(val,5)}'

2nd solution: Without using a variable and using substr itself, though I feel 1st approach is BEST that variable could be used for other tasks also, for fun and as an another option adding this one.

printf '%s' ${tst} | 
awk -F'/' '{print substr(substr($4,5,6),1,2) substr(substr($4,5,6),5)}'

3rd solution: or try this substr also here like:

printf '%s' ${tst} | 
awk -F'/' '{print substr($4,5,2) substr($4,9)}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...