【问题标题】:Awk command to cut the urlawk命令剪切url
【发布时间】:2020-09-14 09:11:16
【问题描述】:

我想把我的网址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

有人可以帮帮我吗?

【问题讨论】:

  • 这一点都不清楚。您的问题中应该始终包含 3 件简单的事情。 1- 代码标签中的输入示例,2- 代码标签中的预期样本,3- 您为解决自己的问题所做的努力,请通过编辑您的问题添加所有内容,然后让我们知道。
  • 所以,你想从 URL 中删除子域部分,只返回域部分???
  • san & ravi,感谢您的回复。我已经发布了 3 个输入。如果用户传递任何类型的输入,它将导致预期的输出。 (origin-ctc-core-nonprod.com)
  • awk '{ print "https://origin-ctc-core-nonprod.com" }'
  • @Luuk 在没有阅读 cmets 的情况下,我只想给出确切的答案 :)

标签: shell awk grep


【解决方案1】:

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

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

您可以使用它:echostring| awk ...、catfile|或awk ...@ 987654330@ .

说明:

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"

再说一遍:

$ var=$(grep -om 1 "origin-ctc-core-nonprod\.com" file) && echo https://$var

【讨论】:

  • Op 说:“如果用户传递任何类型的输入,它将导致预期的输出。(origin-ctc-core-nonprod.com)” ....这不是这样做?
  • 我最初的回答是:while IFS='$\n' read -r line; do echo "https://origin-ctc-core-nonprod.com" ; done :D
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多