Shell Operation Manual

2019-04-17

1. Print Line Number

1
awk '{ print FNR " " $0 }'

2. Use awk to get successive digits in one line

1
echo 'asd1923aa' | awk 'match($0, /[[:digit:]]+/) {print substr($0, RSTART, RLENGTH)}'

3. Replace text between certain lines

  • shell script
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    def replaceTextBetweenLines(textmarkOfUpperLine, textmarkOfBottomLine, toBeReplacedTest, replacement, filepath) {
    upperLineNumber=$(awk '{ print FNR " " $0 }' $filepath | grep "$textmarkOfUpperLine:" | awk '{print $1}')
    bottomLineNumber=$(awk '{ print FNR " " $0 }' $filepath | grep "$blobStorageCredential" | awk '{print $1}')

    sed "$upperLineNumber,$bottomLineNumber s#$toBeReplacedTest#$replacement#g" -i $filepath
    }

    def replaceTextBetweenLines(textmarkOfUpperLine, textmarkOfMiddleLine, textmarkOfBottomLine, toBeReplacedTest, replacement, filepath) {
    upperLineNumber=$(awk '{ print FNR " " $0 }' $filepath | grep "$textmarkOfUpperLine:" | awk '{print $1}')
    bottomLineNumber=$(awk '{ print FNR " " $0 }' $filepath | grep "$blobStorageCredential" | awk '{print $1}')

    sed "$upperLineNumber,$bottomLineNumber s#$toBeReplacedTest#$replacement#g" -i $filepath

    middleLineNumber=$(sed -n "$upperLineNumber,$bottomLineNumber p" $filepath | awk '{ print FNR " " $0 }' | grep $textmarkOfMiddleLine | awk '{ print $1 }')
    upperLineNumber=$((upperLineNumber + middleLineNumber))

    sed "$upperLineNumber,$bottomLineNumber s#$toBeReplacedTest#$replacement#g" -i $filepath
    }
  1. jenkinsfile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    def replaceTextBetweenLines(textmarkOfUpperLine, textmarkOfBottomLine, toBeReplacedTest, replacement, filepath) {
    script {
    upperLineNumber = sh (
    script: "awk '{ print FNR " " \$0 }' ${filepath} | grep \"${textmarkOfUpperLine}:\" | awk '{print \$1}'",
    returnStdout: true
    ).trim()
    bottomLineNumber = sh (
    script: "awk '{ print FNR " " \$0 }' ${filepath} | grep \"${textmarkOfBottomLine}:\" | awk '{print \$1}'",
    returnStdout: true
    ).trim()

    sh "sed '${upperLineNumber},${bottomLineNumber} s#${toBeReplacedTest}#${replacement}#g' -i ${filepath}"
    }
    }

    def replaceTextBetweenLines(textmarkOfUpperLine, textmarkOfMiddleLine, textmarkOfBottomLine, toBeReplacedTest, replacement, filepath) {
    script {
    upperLineNumber = sh (
    script: "awk '{ print FNR " " \$0 }' ${filepath} | grep \"${textmarkOfUpperLine}:\" | awk '{print \$1}'",
    returnStdout: true
    ).trim()
    bottomLineNumber = sh (
    script: "awk '{ print FNR " " \$0 }' ${filepath} | grep \"${textmarkOfBottomLine}:\" | awk '{print \$1}'",
    returnStdout: true
    ).trim()

    middleLineNumber = sh (
    script: "sed -n '${upperLineNumber},${bottomLineNumber} p' ${filepath} | awk '{ print FNR \" \" \$0 }' | grep ${textmarkOfMiddleLine} | awk '{ print \$1 }'",
    returnStdout: true
    ).trim()

    upperLineNumber = sh (
    script: "$((${upperLineNumber} + ${middleLineNumber}))",
    returnStdout: true
    ).trim()

    sh "sed '${upperLineNumber},${bottomLineNumber} s#${toBeReplacedTest}#${replacement}#g' -i ${filepath}"
    }
    }