Search code examples
awkgrepcut

Shell command to cut the url into phase


I want to cut my url https://jenkins-crumbtest2.origin-ctc-core-nonprod.com/ into https://jenkins.origin-ctc-core-nonprod.com:443. I have tried several ways to handle it

tried in these ways

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

Input:

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

Expected Output:

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

Solution

  • Could you please try following, written based on shown samples only.

    awk '{sub(/jenkins-[^.]*/,"jenkins");sub(/\/$/,":443")} 1' Input_file
    

    Or with echo try:

    echo "https://jenkins-crumbtest2.origin-ctc-core-nonprod.com/" |
    awk '{sub(/jenkins-[^.]*/,"jenkins");sub(/\/$/,":443")} 1'
    

    Explanation: sending echo commands output to awk command as an Input then using sub function of awk first substituting everything from keyword Jenkins till dot comes with string Jenkins. Then substituting last occurrence of / with :443 in line.