简体   繁体   中英

How to replace a cron in a shell script using sed?

I need to replace a cron entry in a file using sed or awk.

tried this : didnt work

sed -i 's/0 0 * * 0/0 1 * * 1/g' script.sh

script.sh

#!/bin/bash

mkdir -p .github/workflows

cd .github/workflows
touch semgrep.yml

cat << EOF > semgrep.yml
name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

Kindly help me with the same .

Using mikefarah/yq to edit the file in place ( -i ):

yq -i '.on.schedule[].cron = "0 1 * * 1"' semgrep.yml

would turn a semgrep.yml containing

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

into one containing

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 1 * * 1'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM