AWK命令剪切URL

时间:2020-05-27 10:43:49

标签: shell awk grep

我想将网址https://jenkins-crumbtest2.origin-ctc-core-nonprod.com/切成https://origin-ctc-core-nonprod.com,我尝试了几种方法来处理它

$ echo https://jenkins-crumbtest2-test.origin-ctc-core-nonprod.com/ | cut -d"/" -f3 | cut -d"/" -f5
jenkins-crumbtest2.origin-ctc-core-nonprod.com

我有3个输入,我想通过这些输入以获得预期的输出。我想传递任何输入以获得相同的输出。

输入:

1. https://jenkins-crumbtest2-test.origin-ctc-core-nonprod.com/ (or) 
2. https://jenkins-crumbtest2.origin-ctc-core-nonprod.com/ (or)
3. https://jenkins-crumbtest2-test-lite.origin-ctc-core-nonprod.com/

预期输出:

https://origin-ctc-core-nonprod.com

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

此awk会跳过其中没有固定字符串origin-ctc-core-nonprod.com的记录:

awk 'match($0,/origin-ctc-core-nonprod\.com/){print "https://" substr($0,RSTART,RLENGTH)}'

您可以将其用于:echo string | awk ...cat file { {1}}或| awk ...

表示:

file

或者,如果您不需要awk ' # using awk match($0,/origin-ctc-core-nonprod\.com/) { # if fixed string is matched print "https://" substr($0,RSTART,RLENGTH) # output https:// and fixed string # exit # uncomment if you want only }' # one line of output like in sample 部分,则可以使用https://

grep

然后再次:

grep -om 1 "origin-ctc-core-nonprod\.com"

答案 1 :(得分:2)

能否请您尝试以下。仅使用显示的样品进行书写和测试。

awk '{gsub(/:\/\/.*test\.|:\/\/.*crumbtest2\.|:\/\/.*test-lite\./,"://")} 1' Input_file

上述解决方案的OR非内衬形式如下。

awk '
{
  gsub(/:\/\/.*test\.|:\/\/.*crumbtest2\.|:\/\/.*test-lite\./,"://")
}
1
'  Input_file

说明:在上面添加了详细说明。

awk '                                                                    ##Starting awk program from here.
{
  gsub(/:\/\/.*test\.|:\/\/.*crumbtest2\.|:\/\/.*test-lite\./,"://")     ##Gobally substituting everything till test OR crumbtest OR test-lite with :// in line.
}
1                                                                        ##Printing current line here.
'  Input_file  ##Mentioning Input_file name h